-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentation_server.py
195 lines (154 loc) · 5.82 KB
/
presentation_server.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
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
194
195
import socket
import re
import os
class Presentation_Server:
def __init__(self):
self.port = 8888
self.address = "0.0.0.0"
self.max_num_bytes = 4096
# 3MB = 3145728 bytes
#4096
def create_response_header(self, content: bytes, c_type, s_code):
c_length = len(content)
headers = "HTTP/1.1 %s\r\n" \
"Content-Length: %d\r\n" \
"Content-Type: %s\r\n\r\n" % \
(s_code, c_length, c_type)
# print("return header: ", headers)
return str(headers).encode("utf-8")
def run(self):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# AF_INET : IP socket (IPv4)
# socket.SOCK_STREAM : TCP socket | socket.SOCK_DGRAM = UDP socket
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# allow reuse address and port directly after restart
server_socket.bind((self.address, self.port))
server_socket.listen()
while(True):
connection, client_addr = server_socket.accept()
request = connection.recv(self.max_num_bytes)
request_splitted = request.split(b'\r\n\r\n', maxsplit=1)
request_header = request_splitted[0].decode("utf-8")
request_content = request_splitted[1]
if re.search(r'Content-Length: (.*)\r\n', request_header) is not None:
request_content_length = int(re.search(r'Content-Length: (.*)\r\n', request_header).group(1))
else:
request_content_length = 0
while(len(request_content) < request_content_length):
request_content += connection.recv(self.max_num_bytes)
print('len request: ', len(request))
print('len request header: ', len(request_header))
print('len request content: ', len(request_content))
print('total len request content: ', request_content_length)
print(request_header)
query = request_header.split(" ")[1][1:]
splitquery = query.split("?")
path = splitquery[0]
if request_header.startswith("GET "):
# is the file there?
# print("INFO: trying to read file ", path)
try:
with open(path, "rb") as f:
print("INFO: success reading file: ", path)
s_code = "200 OK"
c_type = ""
# set content type by file extention
if path.endswith(".html"):
c_type = "text/html"
elif path.endswith(".css"):
c_type = "text/css"
elif path.endswith(".js"):
c_type = "text/javascript"
elif path.endswith(".jpg"):
c_type = "image/jpeg"
elif path.endswith(".jpeg"):
c_type = "image/jpeg"
elif path.endswith(".png"):
c_type = "image/jpeg"
else:
c_type = "text/plain"
# file found, so content sources from file
content = f.read()
try:
connection.sendall(
self.create_response_header(content, c_type, s_code))
connection.sendall(content)
except Exception:
print("ERROR: could not send data")
except IOError:
print("ERROR: 404: File not found: ", path)
# we will send a message to the user too
content = b"404 File not found!"
s_code = "404 Not Found"
c_type = "text/plain"
try:
connection.sendall(self.create_response_header(
content, c_type, s_code))
except Exception:
print("ERROR: could not send data")
elif request_header.startswith("POST"):
if(path == "save-elements"):
base_html = ""
with open('base.html', 'rb') as f:
base_html = f.read().decode('utf-8')
imgs_fns = [fn for fn in os.listdir('imgs') if re.search(r'(\.png$|\.jpeg$|\.jpg$)', fn)]
images_html = ''
for img_fn in imgs_fns:
images_html += '<div class="dropdown-option"><label>'
images_html += img_fn
images_html += '</label><img src="imgs/'
images_html += img_fn
images_html += '" alt="some image" draggable="false"></div>'
elements_html = request_content.decode('utf-8')
merged_html = re.sub(r'(<div class="container">)(.*)(</div>)',
r'\1' + elements_html + r'\3',
base_html)
merged_html = re.sub(r'(<div class="dropdown-content">)(.*)(</div>)',
r'\1' + images_html + r'\3',
merged_html)
with open('merged.html', 'wb') as f:
f.write(merged_html.encode('utf-8'))
'''
with open('elements.html', 'wb') as f:
f.write(elements_html.encode('utf-8'))
'''
elif(path == "upload-file"):
print('##### NEW BYTES ########')
request_content_boundary = re.search(r'boundary=(.*)(\r\n)', request_header)
if request_content_boundary is not None:
request_content_boundary = request_content_boundary.group(1) + '\r\n'
parts = request_content.split(request_content_boundary.encode('utf-8'))
for part in parts:
part_splitted = part.split(b'\r\n\r\n', maxsplit=1)
if(len(part_splitted) == 2):
part_header = part_splitted[0].decode('utf-8')
part_content = part_splitted[1]
#print(part_header.decode('utf-8'))
if re.search(r' filename="(.*)"\r\n', part_header) is not None:
filename = re.search(r' filename="(.*)"\r\n', part_header).group(1)
with open('imgs/' + filename, 'wb') as f:
f.write(part_content)
text_response = filename
# we will send a message to the user too
content = text_response.encode('utf-8')
s_code = "200 OK"
c_type = "text/plain"
try:
connection.sendall(self.create_response_header(
content, c_type, s_code))
connection.sendall(content)
except Exception:
print("ERROR: could not send data")
else:
print("###############")
print("THIS PART COULD NOT BE SPLITTED")
print(part)
print("##### END #####")
# close the connection (in any case)
try:
connection.close()
except Exception:
print("ERROR: could not close connection")
if __name__ == "__main__":
server = Presentation_Server()
server.run()