-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
32 lines (27 loc) · 1.06 KB
/
server.py
File metadata and controls
32 lines (27 loc) · 1.06 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
import socket
import re
with open("server.conf") as f:
for line in f:
if re.match(r"^Listen ", line):
ipAddr = re.findall(r"\d+.\d+.\d+.\d", line)[0]
port = int(re.findall(r":\d+", line)[0][1:])
elif re.match(r"^ServerLocation ", line):
serverLocation = "".join(re.findall(r"[^ServerLocation ](?=)\w+", line))
elif re.match(r"^WebsiteFiles ", line):
websiteFiles = "".join(re.findall(r"[^WebsiteFiles ](?=)\w+", line))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
body = "Hello, World! My Python Web Server looks like its working, yay!"
output = f"HTTP/1.1 200 OK\nServer: Cool?\n\
Content-Type: text/html;charset=UTF-8\n\
Content-Length: {len(body.encode('utf-8'))}\
\n\n{body}"
s.bind((ipAddr, port))
print(f"Listening on {ipAddr}:{port}")
s.listen()
while True:
c, addr = s.accept()
data = c.recv(1024)
print(f'Got connection from {addr}')
print("Data sent by client: ", data.decode("utf-8"))
c.sendall(output.encode("utf-8"))
c.close()