-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
35 lines (26 loc) · 811 Bytes
/
client.py
File metadata and controls
35 lines (26 loc) · 811 Bytes
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
import socket
import sys
if __name__ == '__main__':
route = sys.argv[1] if len(sys.argv) > 1 else '/'
HOST = 'localhost'
PORT = 8000
MESSAGE = f'GET {route} HTTP/1.1'
BUFFER_SIZE = 4096
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
print(f"Connected to {HOST}:{PORT}.")
sock.sendall(MESSAGE.encode('utf-8'))
print(f"Sent: {MESSAGE}\n")
response = not None
while (response):
response = sock.recv(BUFFER_SIZE)
if route == '/':
print(f"{response.decode('utf-8')}")
else:
print(f"{response}")
except Exception as e:
print(f"Error: {e}")
finally:
sock.close()
print("Connection closed.")