-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.py
More file actions
193 lines (164 loc) Β· 7.46 KB
/
webserver.py
File metadata and controls
193 lines (164 loc) Β· 7.46 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import sys
import socket
from threading import Thread
from datetime import datetime as date
from proxy_pass import replacement_proxy_pass
OK_RESPONSE = ' 200 OK\r\n'
CREATE_RESPONSE = ' 201 Created\r\n'
NOT_FOUND_RESPONSE = ' 404 Not Found\r\n'
NO_CONTENT_RESPONSE = ' 204 No Content\r\n'
METHOD_NOT_ALLOWED = ' 405 Method Not Allowed\r\n'
FOLDER = '/Users/hastyulia/Documents/GitHub/Python_tasks/'
ALLOW_METHODS = 'Allow: OPTIONS, GET, HEAD, POST, PUT, DELETE\r\n'
HEADS = ('Content-Type: text/html; charset=UTF-8\r\n'
'Transfer-Encoding: chunked\r\n')
class WebServer(Thread):
def __init__(self, client_connection: socket.socket) -> None:
super().__init__()
self.client_connection = client_connection
def run(self) -> None:
request = b''
while True:
body = self.client_connection.recv(2048)
request += body
if body.find(b'\r\n\r\n'):
break
self.http_request_parse(request)
self.client_connection.shutdown(1)
self.client_connection.close()
def http_request_parse(self, request: bytes) -> None:
request = request.decode()
proxy = replacement_proxy_pass(request, self.client_connection)
if proxy:
return
connection_keep_alive = request.find('Connection: keep-alive')
if connection_keep_alive != -1:
self.process_request(request, 'Connection: keep-alive')
request = b''
while True:
try:
body = self.client_connection.recv(2048)
request += body
if body.find(b'\r\n\r\n'):
request = request.decode()
self.process_request(request, 'Connection: keep-alive')
except socket.timeout:
break
else:
self.process_request(request, 'Connection: close')
def process_request(self, request: str, keep_alive: str) -> None:
method = request.split()[0]
if method == 'GET':
self.do_get(request, keep_alive)
elif method == 'HEAD':
self.do_head(request, keep_alive)
elif method == 'POST':
self.do_insert(request, 'a', keep_alive)
elif method == 'DELETE':
self.do_delete(request, keep_alive)
elif method == 'PUT':
self.do_insert(request, 'w', keep_alive)
elif method == 'OPTIONS':
self.do_options(request, keep_alive)
else:
response = (request.split()[2] + METHOD_NOT_ALLOWED
+ date.strftime(date.now(), '%a, %d %b %Y %H:%M:%S ')
+ 'GMT\r\n' + HEADS + '\r\n\r\n').encode()
self.client_connection.sendall(response)
def do_get(self, request: str, keep_alive: str) -> None:
split_request = request.split()
path = os.path.join(FOLDER, split_request[1])
http_version = split_request[2]
try:
with open(path, 'r') as file:
response = (http_version + OK_RESPONSE + date.strftime(
date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + '\r\n\r\n').encode()
self.client_connection.sendall(response)
self.client_connection.sendall(file.read().encode())
except IOError:
response = (http_version + NOT_FOUND_RESPONSE + date.strftime(
date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + '\r\n\r\n').encode()
self.client_connection.sendall(response)
def do_head(self, request: str, keep_alive: str) -> None:
split_request = request.split()
path = os.path.join(split_request[1])
http_version = split_request[2]
try:
with open(path) as _:
response = (http_version + OK_RESPONSE + date.strftime(
date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + '\r\n\r\n').encode()
except IOError:
response = (http_version + NOT_FOUND_RESPONSE + date.strftime(
date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + '\r\n\r\n').encode()
self.client_connection.sendall(response)
def do_insert(self, request: str, mode: str, keep_alive: str) -> None:
while True:
body = self.client_connection.recv(2048)
request += body.decode()
if body.find(b'\r\n\r\n'):
break
split_request = request.split('\r\n\r\n')
head = split_request[0]
body = split_request[1]
path = os.path.join(head.split()[1])
http_version = head.split()[2]
location = f'Content-Location: {path}\r\n'
try:
with open(path, mode) as file:
file.read(1)
file.write(body)
response = (http_version + OK_RESPONSE + date.strftime(
date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + location
+ '\r\n\r\n').encode()
except IOError:
with open(path, 'w') as file:
file.write(body)
response = (http_version + CREATE_RESPONSE + date.strftime
(date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + location
+ '\r\n\r\n').encode()
self.client_connection.sendall(response)
def do_delete(self, request: str, keep_alive: str) -> None:
split_request = request.split()
path = os.path.join(split_request[1])
http_version = split_request[2]
try:
os.remove(path)
response = (http_version + NO_CONTENT_RESPONSE + date.strftime
(date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + '\r\n\r\n').encode()
except IOError:
response = (http_version + NOT_FOUND_RESPONSE + date.strftime
(date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n'
+ HEADS + keep_alive + '\r\n\r\n').encode()
self.client_connection.sendall(response)
def do_options(self, request: str, keep_alive: str) -> None:
split_request = request.split()
http_version = split_request[2]
response = (http_version + OK_RESPONSE + ALLOW_METHODS + date.strftime(
date.now(), '%a, %d %b %Y %H:%M:%S ') + 'GMT\r\n' + HEADS
+ keep_alive + '\r\n\r\n').encode()
self.client_connection.sendall(response)
def listen(host='127.0.0.1', port=17000) -> None:
browser_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
browser_connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
browser_connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
browser_connection.bind((host, port))
browser_connection.listen(20)
while True:
current_connection, address = browser_connection.accept()
server = WebServer(current_connection)
server.start()
if __name__ == '__main__':
proxy_host = input('Enter proxy host: ')
proxy_port = int(input('Enter proxy port: '))
try:
listen(proxy_host, proxy_port)
except KeyboardInterrupt:
sys.exit()