-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleServer.py
More file actions
executable file
·59 lines (49 loc) · 1.7 KB
/
Copy pathSimpleServer.py
File metadata and controls
executable file
·59 lines (49 loc) · 1.7 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
#!/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 svm
def handleoptimize(jsdict):
if 'x' in jsdict and 'y' in jsdict:
print 'Inside handle optimize!'
print jsdict['x']
print jsdict['y']
print jsdict['gamma']
results = svm.optimize(jsdict['x'], jsdict['y'], jsdict['gamma'])
print results
return results
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
if self.path == '/svmexample':
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)
jsdict = 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()