-
Notifications
You must be signed in to change notification settings - Fork 0
/
servidor.py
47 lines (37 loc) · 1.29 KB
/
servidor.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
42
43
44
45
46
47
import socket
from pathlib import Path
from utils import extract_route, read_file, build_response
from views import index, delete, update, paginaedit,home,erro
import re
CUR_DIR = Path(__file__).parent
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8080
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()
print(f'Servidor escutando em (ctrl+click): http://{SERVER_HOST}:{SERVER_PORT}')
while True:
client_connection, client_address = server_socket.accept()
request = client_connection.recv(1024).decode()
print('*'*100)
print(request)
route = extract_route(request)
filepath = CUR_DIR / route
if filepath.is_file():
response = build_response() + read_file(filepath)
elif route == '':
response = index(request)
elif re.match(r'^delete\d+$', route):
response= delete(request)
elif re.match(r'^edit\d+$',route):
response=paginaedit(request,route)
elif route=='home':
response=home()
elif route=="update":
response=update(request,route)
else:
response = erro()
client_connection.sendall(response)
client_connection.close()
server_socket.close()