-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
76 lines (53 loc) · 1.84 KB
/
webserver.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from http.server import SimpleHTTPRequestHandler as Handler
from http.server import HTTPServer as Server
from threading import Thread
import socket
import os
PORT = int(os.getenv('PORT', 3000))
http = Server(("", PORT), Handler)
class WebServer:
def __init__(self, address='0.0.0.0', port=PORT):
self.port = port
self.address = address
def start(self):
# Definindo o tipo de server como 'sr' que será TCP/IP
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sr:
sr.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sr.bind((self.address, self.port))
sr.listen(10)
while True:
print('Aguardando conexão...')
# Definindo o sr para aguardar conexão, ele vai continuar ligado
conn, addr = sr.accept()
req = HttpRequest(conn, addr)
req.start()
class HttpRequest(Thread):
def __init__(self, conn, addr):
super(HttpRequest, self).__init__()
self.conn = conn
self.addr = addr
self.CRLF = '\r\n'
self.buffer_size = 4096
def run(self):
request = self.conn.recv(self.buffer_size)
print(request)
response = HttpResponse(self.conn, self.addr, '')
response.processRespose()
self.conn.close()
class pageWeb:
os.chdir('src')
try:
print("Conexão iniciada na porta %i" % PORT)
http.serve_forever()
except KeyboardInterrupt:
pass
except socket.error as error:
print("Algo deu errado!", error)
http.server_close()
class HttpResponse:
def __init__(self, conn, addr, file):
self.conn = conn
self.addr = addr
self.file = file
def processRespose(self):
self.conn.sendall(pageWeb)