-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.py
43 lines (34 loc) · 1.18 KB
/
post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import socket
def send_post_request():
host = "localhost"
port = 5000
# יצירת סוקט
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
# נתונים לשליחה
params = "param1453=value1¶m500=value250"
content_length = len(params)
# request of post is just the request itself, and after it headers and after it the parameters
# (what called body).
# בניית בקשת POST ידנית
request = f"POST /api HTTP/1.1\r\n" \
f"Host: {host}\r\n" \
f"Content-Type: application/x-www-form-urlencoded\r\n" \
f"Content-Length: {content_length}\r\n" \
f"Connection: close\r\n\r\n" \
f"{params}"
# שליחת הבקשה
client_socket.sendall(request.encode())
# קבלת תשובת השרת
response = b""
while True:
part = client_socket.recv(1024)
if not part:
break
response += part
# הצגת תשובת השרת
print(response.decode("utf-8"))
# סגירת החיבור
client_socket.close()
if __name__ == "__main__":
send_post_request()