-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
97 lines (90 loc) · 3.84 KB
/
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
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
import threading
import logging
import time
import ssl
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
class Handler(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
print(threading.current_thread())
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self._set_response()
with open('./login.html', 'rb') as file:
self.wfile.write(file.read())
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
print(threading.current_thread())
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", str(self.path), str(self.headers), post_data.decode('utf-8'))
if('sign_up' in post_data.decode("utf-8")):
tmp = post_data.decode('utf-8').split('&')
username = tmp[0].split('=')[1]
password = tmp[1].split('=')[1]
htmlfile = './signupsuccess.html'
with open('./accounts.txt', 'rb') as file:
if (username == '') or (password == '') or (username + '$') in file.read().decode("utf-8"):
htmlfile = './signupfailed.html'
file.close()
else:
file.close()
with open('./accounts.txt', 'a') as file:
file.write((username + '$' + password) + '\n')
file.close()
self._set_response()
with open(htmlfile, 'rb') as file:
self.wfile.write(file.read())
file.close()
elif('sign_in' in post_data.decode("utf-8")):
tmp = post_data.decode('utf-8').split('&')
username = tmp[0].split('=')[1]
password = tmp[1].split('=')[1]
htmlfile = './signinfailed.html'
with open('./accounts.txt', 'rb') as file:
if username != '' and password != '' and (username + '$' + password) in file.read().decode("utf-8"):
htmlfile = './index.html'
file.close()
comment = ''
with open('./comments.txt', 'r') as file:
comment = file.read()
file.close()
self._set_response()
with open(htmlfile, 'rb') as file:
tmp = file.read().decode("utf-8").format(text=comment).encode()
self.wfile.write(tmp)
file.close()
elif('comment' in post_data.decode("utf-8")):
tmp = post_data.decode('utf-8').split('=')
comment = tmp[1]
with open('./comments.txt', 'a') as file:
file.write(comment + '<br>\n')
file.close()
comment = ''
with open('./comments.txt', 'r') as file:
comment = file.read()
file.close()
self._set_response()
with open('./index.html', 'rb') as file:
self.wfile.write(file.read().decode("utf-8").format(text=comment).encode())
file.close()
else:
self._set_response()
with open('./error.html', 'rb') as file:
self.wfile.write(file.read())
file.close()
if __name__ == '__main__':
with open('./accounts.txt', 'wb') as file:
file.truncate()
file.close()
with open('./comments.txt', 'wb') as file:
file.truncate()
file.close()
server = ThreadedHTTPServer(('', 8080), Handler)
server.socket = ssl.wrap_socket(server.socket, certfile='./server.pem', server_side=True)
print('start')
server.serve_forever()