-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
38 lines (31 loc) · 1.07 KB
/
server.py
File metadata and controls
38 lines (31 loc) · 1.07 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
import socket
import threading
from handleRequest import *
from sendResponse import *
def handler(conn : socket.socket, address):
print(f"-------------------\n [SERVER]\n {address} sent request to server.\n")
request = Request()
request.getRequest(conn)
if len(request.get_header()) != 0:
print(f"-------------------\n [SERVER]\n Request recieved")
if request.get_method() == 'GET':
do_GET(conn, request.get_path())
elif request.get_method() == 'POST':
do_POST(conn, request.get_path(), request.get_content())
conn.close()
def main():
host = "localhost"
port = 8080
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', port))
server.listen(5)
print(f"-------------------\n [SERVER]\n Listening on: {host} {port}")
cnt = 1
while 1:
(conn, address) = server.accept()
thread = threading.Thread(target=handler, args=(conn, address))
print(f" Thread {cnt}\n")
cnt += 1
thread.start()
if __name__ == '__main__':
main()