-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (29 loc) · 1.13 KB
/
main.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
import socket
SERVER_HOST = "0.0.0.0"
SERVER_PORT = 8095
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((SERVER_HOST,SERVER_PORT))
server_socket.listen(1)
print(f"Listening on port {SERVER_PORT} ...")
while True:
client_socket, client_address = server_socket.accept()
request = client_socket.recv(150000).decode()
print(request)
headers = request.split('\n')
print(headers[0])
first_header_components = headers[0].split()
http_method = first_header_components[0]
path = first_header_components[1]
if http_method == 'GET':
if path == '/':
fin = open('index.html')
elif path =='/servers':
fin = open('servers.html')
content = fin.read()
fin.close()
response = 'HTTP/1.1 200 OK\n\n'+ content
else:
response = 'HTTP/1.1 405 Method Not Allowed\n\nAllow: GET,'
client_socket.sendall(response.encode())
client_socket.close()