forked from cmaes/tankertrucks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleServer.py
executable file
·51 lines (42 loc) · 1.49 KB
/
SimpleServer.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
#!/usr/bin/python
import os
import sys
import SimpleHTTPServer
import SocketServer
import logging
import cgi
import json
from subprocess import Popen, PIPE, STDOUT
if 'PORT' in os.environ:
PORT = int(os.environ['PORT'])
print 'Got port ', PORT
else:
PORT = 8000
import tankertrucks
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
if self.path == '/tankertrucks.py':
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'application/json':
length = int(self.headers.getheader('content-length'))
data = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
for val in data:
jsdict = json.loads(val)
print 'Got data'
print jsdict
jsdict = tankertrucks.handleoptimize(jsdict)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(jsdict))
return
else:
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = ServerHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Starting simple server"
print "serving at port", PORT
httpd.serve_forever()