-
Notifications
You must be signed in to change notification settings - Fork 63
/
serve_practice.py
38 lines (31 loc) · 1.35 KB
/
serve_practice.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
#!/usr/bin/env python
import BaseHTTPServer
import SimpleHTTPServer
import json
import subprocess
import re
PORT = 8000
class CheckerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_POST(self):
def resp(statusCode, result):
self.send_response(statusCode)
self.end_headers()
self.wfile.write(json.dumps(result))
if self.path == '/check':
try:
input = json.loads(self.rfile.read(int(self.headers.getheader('content-length'))))
challName = input['chall']
if not re.match('^[a-zA-Z0-9_]+$', challName):
resp(400, {'status': 'error', 'error': 'badChallName'})
return
with open('user.ksy', 'wt') as f: f.write(input['yaml'])
checkRes = subprocess.check_output('node checker.js user.ksy practice\%s\input.bin practice\%s\check.json' % (challName, challName))
print 'checkRes %r', checkRes
resp(200, {'status': 'ok', 'check_res': json.loads(checkRes)})
except Exception as e:
print e
resp(400, {'status': 'exception'})
else:
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_POST(self)
if __name__ == "__main__":
BaseHTTPServer.HTTPServer(("", PORT), CheckerHandler).serve_forever()