-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebservercgi.py
290 lines (246 loc) · 8.51 KB
/
webservercgi.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import errno
import os
import signal
import socket
import cgi
import pymysql.cursors
SERVER_ADDRESS = (HOST, PORT) = '', 8888
REQUEST_QUEUE_SIZE = 1024
def grim_reaper(signum, frame):
while True:
try:
pid, status = os.waitpid(
-1, # Wait for any child process
os.WNOHANG # Do not block and return EWOULDBLOCK error
)
except OSError:
return
if pid == 0: # no more zombies
return
def write_header(client_connection):
client_connection.sendall(b"HTTP/1.1 200 Document Follows\r\n")
client_connection.sendall(b"Server: CS 252 lab 6\r\n")
def write_content_type(client_connection):
client_connection.sendall(b"Content-Type: text/html\r\n\r\n")
def write_content(client_connection):
client_connection.sendall(b"""<!DOCTYPE html>
<html>
<head>
<title>Pixelgraph: Welcome!</title>
</head>
<body>
<form method=\"post\" >
Username: <br>
<input type=\"text\" name=\"username\"><br>
Password: <br>
<input type=\"password\" name=\"password\"><br>
<input type=\"submit\" value=\"Submit\"><br>
</form>
<form method=\"post\">
<input type=\"hidden\" name=\"-newacct\">
<input type=\"submit\" value=\"Create Account\"><br>
</form>
</body>
</html> """)
def write_contentnomatch(client_connection):
client_connection.sendall(b"""<!DOCTYPE html>
<html>
<head>
<title>Pixelgraph: Welcome!</title>
</head>
<body>
Error: Passwords Do Not Match!<br>
<form method=\"post\" >
Choose Username: <br>
<input type=\"text\" name=\"_newusername\"><br>
Choose Password: <br>
<input type=\"password\" name=\"_newpassword\"><br>
Repeat Password: <br>
<input type=\"password\" name=\"password\"><br>
<input type=\"submit\" value=\"Submit\"><br>
</form>
</body>
</html> """)
def write_contentwrong(client_connection):
client_connection.sendall(b"""<!DOCTYPE html>
<html>
<head>
<title>Pixelgraph: Welcome!</title>
</head>
<body>
Wrong Username or Password! Try again or create a new user:<br>
<form method=\"post\" >
Username: <br>
<input type=\"text\" name=\"username\"><br>
Password: <br>
<input type=\"password\" name=\"password\"><br>
<input type=\"submit\" value=\"Submit\"><br>
</form>
<form method=\"post\">
<input type=\"hidden\" name=\"-newacct\">
<input type=\"submit\" value=\"Create Account\"><br>
</form>
</body>
</html> """)
def write_contentnew(client_connection):
client_connection.sendall(b"""<!DOCTYPE html>
<html>
<head>
<title>Pixelgraph: Welcome!</title>
</head>
<body>
<form method=\"post\" >
Choose Username: <br>
<input type=\"text\" name=\"_newusername\"><br>
Choose Password: <br>
<input type=\"password\" name=\"_newpassword\"><br>
Repeat Password: <br>
<input type=\"password\" name=\"password\"><br>
<input type=\"submit\" value=\"Submit\"><br>
</form>
</body>
</html> """)
def write_contentgraph(client_connection, user, passw):
connection = pymysql.connect(host='localhost',user='root', password='gustavo', db='art', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
client_connection.sendall(b"""<!DOCTYPE html>
<html>
<head>
<title>Pixelgraph: Welcome!</title>
</head>
<body>
put graph here!
username/password = """)
client_connection.sendall(user.encode())
client_connection.sendall(passw.encode())
client_connection.sendall(b"""
</body>
</html> """)
def write_contentgraphdelay(client_connection, user, passw):
connection = pymysql.connect(host='localhost',user='root', password='gustavo', db='art', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
client_connection.sendall(b"""<!DOCTYPE html>
<html>
<head>
<title>Pixelgraph: Welcome!</title>
</head>
<body>
No graph changes allowed yet, wait 2 minutes!
username/password = """)
client_connection.sendall(user.encode())
client_connection.sendall(passw.encode())
client_connection.sendall(b"""
</body>
</html> """)
def handle_request(client_connection):
request = client_connection.recv(1024)
decoded = request.decode()
a = decoded.split()
print(decoded)
geta = a[-1]
http_response = b"""\
HTTP/1.1 200 OK
Hello, World!
"""
#client_connection.sendall(http_response)
write_header(client_connection)
write_content_type(client_connection)
if geta.startswith("-"):
write_contentnew(client_connection)
elif geta.startswith("u"):
b = geta.split("&")
user = b[0]
user = user[9:]
passw = b[1]
passw = passw[9:]
print(user)
print(passw)
print("testing username: " + user + " and password: " + passw + "\n")
connection = pymysql.connect(host='localhost',user='root', password='gustavo', db='art', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "SELECT `username`, `password` FROM `users` WHERE `username`=%s AND `password`=%s"
cursor.execute(sql, (user, passw))
result = cursor.fetchone()
print(result)
if result != None:
sql = "SELECT * FROM users WHERE `username`=%s AND `timeposted` <= NOW() - INTERVAL 2 MINUTE"
cursor.execute(sql, (user))
result = cursor.fetchone()
print(result)
if result != None:
write_contentgraph(client_connection, user, passw)
sql = "UPDATE users SET timeposted = NOW() where username=%s"
cursor.execute(sql, (user))
connection.commit()
else:
write_contentgraphdelay(client_connection, user, passw)
else:
write_contentwrong(client_connection)
finally:
connection.close()
#write_contentgraph(client_connection, user, passw)
elif geta.startswith("_"):
b = geta.split("&")
user = b[0]
user = user[13:]
passw = b[1]
passw = passw[13:]
passwt = b[2]
passwt = passwt[9:]
print(user)
print(passw)
if passwt == passw :
print("adding user " + user + "with password " + passw)
connection = pymysql.connect(host='localhost',user='root', password='gustavo', db='art', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `users` (`username`, `password`, `timeposted`) VALUES (%s, %s, NOW() - INTERVAL 2 MINUTE)"
cursor.execute(sql, (user, passw))
connection.commit()
sql = "UPDATE users SET timeposted = NOW() where username=%s"
cursor.execute(sql, (user))
write_contentgraph(client_connection, user, passw)
connection.commit()
finally:
connection.close()
# write_contentgraph(client_connection, user, passw)
else:
write_contentnomatch(client_connection)
else:
write_content(client_connection)
def checkGET(checkString):
if "GET" in checkString:
return True
else:
return False
def checkPOST(checkString):
if "POST" in checkString:
return True
else:
return False
def serve_forever():
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind(SERVER_ADDRESS)
listen_socket.listen(REQUEST_QUEUE_SIZE)
print('Serving HTTP on port {port} ...'.format(port=PORT))
signal.signal(signal.SIGCHLD, grim_reaper)
while True:
try:
client_connection, client_address = listen_socket.accept()
except IOError as e:
code, msg = e.args
# restart 'accept' if it was interrupted
if code == errno.EINTR:
continue
else:
raise
pid = os.fork()
if pid == 0: # child
listen_socket.close() # close child copy
handle_request(client_connection)
client_connection.close()
os._exit(0)
else: # parent
client_connection.close() # close parent copy and loop over
if __name__ == '__main__':
serve_forever()