forked from Shariful-Islam-Sourav/phq_http_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
52 lines (41 loc) · 1.2 KB
/
Copy pathclient.py
File metadata and controls
52 lines (41 loc) · 1.2 KB
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
44
45
46
47
48
49
50
51
52
"""
Course: Computer Networks
Project: HTTP Client
Team: PHQ
Implemented by: Ahammed Tanim
Role: Client UI & Command Line Interface
"""
import socket
import sys
HOST = '127.0.0.1'
PORT = 8080
def get_page(filename):
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
# Standard HTTP GET Request
request = (
f"GET /{filename} HTTP/1.1\r\n"
f"Host: {HOST}\r\n"
f"Connection: close\r\n"
f"\r\n"
)
client.sendall(request.encode('utf-8'))
response = b""
while True:
chunk = client.recv(4096)
if not chunk:
break
response += chunk
print(f"--- Team PHQ Client: Requesting /{filename} ---")
print(response.decode('utf-8', errors='replace'))
print("\n-------------------------------------------")
except ConnectionRefusedError:
print("Error: Could not connect. Make sure server.py is running.")
finally:
client.close()
if __name__ == "__main__":
file_to_get = "index.html"
if len(sys.argv) > 1:
file_to_get = sys.argv[1]
get_page(file_to_get)