-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
438 lines (367 loc) · 16.3 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import sys
import MolDisplay
import molecule
import molsql
import io
import requests
import json
import urllib
import cgi
from http.server import HTTPServer, BaseHTTPRequestHandler
#creating database
db = molsql.Database(reset=True)
db.create_tables()
class MyHandler(BaseHTTPRequestHandler):
elementList = []
elementList2 = []
moleculeList = []
message = ""
elementsMessage = ""
#presents a web-form to upload files with path is "/" and generates a 404 error otherwise
def do_GET(self):
#path to home page
if self.path == "/":
self.send_response(200)
f = open("html/homePage.html")
str_ = f.read()
self.send_header("Content-type", "text/html")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to link JS for addRemove page
elif self.path == "/scripts/addRemoveScript.js":
self.send_response(200)
f = open("scripts/addRemoveScript.js")
str_ = f.read()
self.send_header("Content-type", "text/javascript")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to link JS for uploadSDf page
elif self.path == "/scripts/uploadSDFScript.js":
self.send_response(200)
f = open("scripts/uploadSDFScript.js")
str_ = f.read()
self.send_header("Content-type", "text/javascript")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to link JS for selectMolecules page
elif self.path == "/scripts/selectMoleculesScript.js":
self.send_response(200)
f = open("scripts/selectMoleculesScript.js")
str_ = f.read()
self.send_header("Content-type", "text/javascript")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to link style sheets for uploadFile page
elif self.path == "/css/uploadFile.css":
self.send_response(200)
f = open("css/uploadFile.css")
str_ = f.read()
self.send_header("Content-type", "text/css")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to link style sheets for addRemove page
elif self.path == "/css/addRemove.css":
self.send_response(200)
f = open("css/addRemove.css")
str_ = f.read()
self.send_header("Content-type", "text/css")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to link style sheets for homePage
elif self.path == "/css/homePage.css":
self.send_response(200)
f = open("css/homePage.css")
str_ = f.read()
self.send_header("Content-type", "text/css")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to link style sheets for selecting molecules
elif self.path == "/css/selectMolecules.css":
self.send_response(200)
f = open("css/selectMolecules.css")
str_ = f.read()
self.send_header("Content-type", "text/css")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to display table after an element is removed
elif self.path == "/removedData":
#parsing table values so they can be sent back to JS
myStr = db.conn.execute( "SELECT * FROM Elements;" ).fetchall()
elementNum = []
elementCode = []
elementName = []
color1 = []
color2 = []
color3 = []
radius = []
toWrite = []
#contatanating all the data into a temp string to right
for i in myStr:
temp = "Element Number: "+ str(i[0]) + " - Element Code: " + i[1] + " - Element Name: " + i[2] + " - Colors: " + i[3] + " " + i[4] + " "+ i[5] + " - Radius: "+ str(i[6])
toWrite.append(temp) #adding string to a list of strings
#joining the list of strings to a comma seperated string
my_string = ','.join(toWrite)
#sending string back to js
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header("Content-length", len(my_string))
self.end_headers()
self.wfile.write(my_string.encode("utf-8"))
#path to add and remove elements from the system
elif self.path == "/addRemove?":
self.send_response(200)
f = open("html/addRemove.html")
str_ = f.read()
self.send_header("Content-type", "text/html")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to upload sdf files to the system
elif self.path == "/uploadFile?":
self.send_response(200)
f = open("html/uploadSDF.html")
str_ = f.read()
self.send_header("Content-type", "text/html")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#path to select from a list of molecules
elif self.path == "/selectMol?":
self.send_response(200)
f = open("html/selectMolecules.html")
str_ = f.read()
self.send_header("Content-type", "text/html")
self.send_header("Content-length", len(str_))
self.end_headers()
f.close()
self.wfile.write(bytes(str_, "utf-8"))
#adding molecules to the data base
elif self.path == "/addedMolecules":
myMols = []
atomNos = []
bondNos = []
molStrings = []
cursor = db.conn
#get all molecule names from the data base
molNames = cursor.execute( "SELECT Molecules.NAME FROM Molecules;").fetchall()
#loading molecules and adding each molecule to a list of molecules
for i in molNames:
myMols.append(db.load_mol(i[0]))
#getting each molecules atom and bond numbers and adding them to a list of atom and bond numbers
for i in myMols:
atomNos.append(i.atom_no)
bondNos.append(i.bond_no)
#creating a temp string with the molecules name, atom nums, and bond nums, and adding them to a list of molecule strings
for i in range(len(molNames)):
temp = molNames[i][0] + " " + str(atomNos[i]) + " " + str(bondNos[i])
molStrings.append(temp)
#joining molecule strings to be one string
my_string = ','.join(molStrings)
#sending string to be used in JS
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header("Content-length", len(my_string))
self.end_headers()
self.wfile.write(my_string.encode("utf-8"))
#path if user enters an invalid file, sends an error message
elif self.path == "/invalidFile":
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(MyHandler.message.encode('utf-8'))
#path if user gives invalid element infor, sends an error message
elif self.path == "/invalidElement":
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(MyHandler.elementsMessage.encode('utf-8'))
else:
self.send_response(404)
self.end_headers()
self.wfile.write(bytes("404: not found", "utf-8"))
def do_POST(self):
#post request from addRemoveScript goes here when user wants to add an element
if self.path == "/added":
self.send_response(200)
#reading data from JS
content_length = int(self.headers.get("Content-Length"))
data = self.rfile.read(content_length)
#parsing JSON to a python dictonary
dict = json.loads(data)
#accessing values from dictonary
elementNum = dict["elementNum"]
elementCode = dict["elementCode"]
elementName = dict["elementName"]
color1 = dict["colors"][0]
color2 = dict["colors"][1]
color3 = dict["colors"][2]
radius = dict["radius"]
#adding to element to elements table
try:
db['Elements'] = (elementNum,elementCode,elementName,color1,color2,color3,radius)
MyHandler.elementsMessage = ""
except:
MyHandler.elementsMessage = "invalid"
#post request from addRemoveScript goes here when user wants to remove an element
elif self.path == "/removed":
self.send_response(200)
#reading data from JS
content_length = int(self.headers.get("Content-Length"))
data = self.rfile.read(content_length)
#parsing JSON to a python dictonary
dict = json.loads(data)
#accessing values from dictonary
elementNum = dict["elementNum"]
elementCode = dict["elementCode"]
elementName = dict["elementName"]
color1 = dict["colors"][0]
color2 = dict["colors"][1]
color3 = dict["colors"][2]
radius = dict["radius"]
#removing element from table
query = "DELETE FROM Elements WHERE ELEMENT_CODE = (?)"
params = elementCode
#making sure data to remove is not malicious
db.conn.execute(query,params)
db.conn.commit()
#path when user has uploaded SDf file and given molecule a name
elif self.path == "/uploadedSDF":
#parsing form data
form = cgi.FieldStorage(
fp = self.rfile,
headers = self.headers,
environ = {'REQUEST_METHOD':'POST'}
)
#getting file
file_item = form['sdfFile'].file
#getting mol name
molName = form.getvalue('molName')
#reading file
asBytes = file_item.read()
#turning file to bytes
bytes_io = io.BytesIO(asBytes)
#turning file to text io
text_io = io.TextIOWrapper(bytes_io)
#checking if data is malicious
try:
db.add_molecule(molName,text_io)
MyHandler.message = ""
except:
MyHandler.message = "invalid"
self.send_response(200)
#path when user wants to display a molecule
elif self.path == "/displayMol":
content_length = int(self.headers.get("Content-Length"))
data = self.rfile.read(content_length)
#parsing JSON to a python dictonary
dict = json.loads(data)
name = dict["molName"]
#loading the molecules
myMol = db.load_mol(name)
#getting the radius elements and radial gradients
MolDisplay.radius = db.radius()
MolDisplay.element_name = db.element_name()
MolDisplay.header += db.radial_gradients()
#getting the svg
myMol.sort()
svg = myMol.svg()
message = svg
#sending the svg back to the server
self.send_response( 200 );
self.send_header( "Content-type", "text/plain" )
self.send_header( "Content-length", len(message) )
self.end_headers()
self.wfile.write( bytes( message, "utf-8" ) )
#path for xrotation
elif self.path == "/xRotation":
#reading data from JS
content_length = int(self.headers.get("Content-Length"))
data = self.rfile.read(content_length)
#parsing JSON to a python dictonary
dict = json.loads(data)
#getting molecule name and loading molecule
molName = dict["name"]
mol = db.load_mol(molName)
#performing a rotation of that molecule 90 degress around the x axis
mx = molecule.mx_wrapper(90,0,0)
mol.xform(mx.xform_matrix)
#getting the svg of the rotated molecule
mol.sort()
svg = mol.svg()
message = svg
self.send_response( 200 );
self.send_response( 200 );
self.send_header( "Content-type", "text/plain" )
self.send_header( "Content-length", len(message) )
self.end_headers()
self.wfile.write( bytes( message, "utf-8" ) )
#path for yrotation
elif self.path == "/yRotation":
#reading data from JS
content_length = int(self.headers.get("Content-Length"))
data = self.rfile.read(content_length)
#parsing JSON to a python dictonary
dict = json.loads(data)
#getting the name of the molecule and loading the molecule
molName = dict["name"]
mol = db.load_mol(molName)
#performing a rotation of the molecule 90 degrees around the y axis
mx = molecule.mx_wrapper(0,90,0)
mol.xform(mx.xform_matrix)
#getting the svg of the rotated molecule
mol.sort()
svg = mol.svg()
message = svg
self.send_response( 200 );
self.send_response( 200 );
self.send_header( "Content-type", "text/plain" )
self.send_header( "Content-length", len(message) )
self.end_headers()
self.wfile.write( bytes( message, "utf-8" ) )
#path for zrotation
elif self.path == "/zRotation":
#reading data from JS
content_length = int(self.headers.get("Content-Length"))
data = self.rfile.read(content_length)
#parsing JSON to a python dictonary
dict = json.loads(data)
#getting the molecule name and loading the molecule
molName = dict["name"]
mol = db.load_mol(molName)
#perfoming a rotation of the molecule 90 degrees around the z axis
mx = molecule.mx_wrapper(0,0,90)
mol.xform(mx.xform_matrix)
#getting the svg of the rotated molecule
mol.sort()
svg = mol.svg()
message = svg
self.send_response( 200 );
self.send_response( 200 );
self.send_header( "Content-type", "text/plain" )
self.send_header( "Content-length", len(message) )
self.end_headers()
self.wfile.write( bytes( message, "utf-8" ) )
else:
self.send_response(404)
self.end_headers()
self.wfile.write(bytes("404: not found", "utf-8"))
httpd = HTTPServer(("localhost", int(sys.argv[1])), MyHandler)
httpd.serve_forever()