forked from openmm/pdbfixer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuiserver.py
More file actions
70 lines (59 loc) · 2.25 KB
/
uiserver.py
File metadata and controls
70 lines (59 loc) · 2.25 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
from threading import Thread
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from urlparse import parse_qs
import cgi
class _Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.hasSentResponse = False
if callback is not None:
queryStart = self.path.find('?')
if queryStart > -1:
parameters = parse_qs(self.path[queryStart+1:])
else:
parameters = {}
self.invokeCallback(parameters)
if not self.hasSentResponse:
self.sendResponse(content)
def do_POST(self):
self.hasSentResponse = False
parameters = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type']})
self.invokeCallback(parameters)
if not self.hasSentResponse:
self.sendResponse(content)
def log_message(self, format, *args):
return
def invokeCallback(self, parameters):
path = self.path
if '?' in path:
path = path[:path.find('?')]
if path in callback:
callback[path](parameters, self)
def sendResponse(self, response, type="text/html"):
self.hasSentResponse = True
self.send_response(200)
self.send_header("Content-type", type)
self.send_header("Content-length", str(len(response)))
self.end_headers()
self.wfile.write(response)
def sendDownload(self, download, filename):
self.hasSentResponse = True
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header("Content-length", str(len(download)))
self.send_header("Content-Disposition", 'attachment; filename="%s"' % filename)
self.end_headers()
self.wfile.write(download)
class _ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
pass
content = ""
callback = {}
server = _ThreadingHTTPServer(("localhost", 8000), _Handler)
def beginServing():
Thread(target=server.serve_forever).start()
def setContent(newContent):
global content
content = newContent
def setCallback(newCallback, path="/"):
global callback
callback[path] = newCallback