-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple_server.py
94 lines (78 loc) · 2.71 KB
/
simple_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
import cv2
import SocketServer
import urllib
from os import remove
from time import time
from BaseHTTPServer import BaseHTTPRequestHandler
from urlparse import parse_qs
from src.options import DEBUG, API_KEYS
from src.raw_photo import RawPhoto
PORT = 8012
URL_HEAD = '/scanner/check-now'
##
# requires `options.py` in src which contains API_KEY and DEBUG option
##
class RequestHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
if DEBUG:
print(self.path, self.path[:len(URL_HEAD)])
# has to start with URL_HEAD
if self.path[:len(URL_HEAD)] != URL_HEAD:
print('> != URL_HEAD')
return
# has to have GET parameter
if len(self.path) <= len(URL_HEAD) + 1: # 1 filters `?`
print('> NO GET PARAMETER')
return
# has to have url in GET parameter
get_param = parse_qs(self.path[len(URL_HEAD) + 1:])
if 'url' not in get_param or 'key' not in get_param or \
'num_papers' not in get_param or 'num_questions' not in get_param:
print('> `url`, `key`, `num_papers`, OR `num_questions` NOT IN PARAMETERS')
return
url = get_param['url'][0]
key = get_param['key'][0]
num_papers = get_param['num_papers'][0]
num_questions = get_param['num_questions'][0]
if key not in API_KEYS:
print('> WRONG API KEY')
return
try:
num_papers = int(num_papers)
num_questions = int(num_questions)
except:
print('> CANNOT PARSE `num_papers` OR `num_questions` TO INTEGERS')
return
print('> PROCESSING REQUEST...')
try:
print(' - downloading raw photo')
filepath = 'tmp/%d.jpg' % int(time() * 1000)
f = open(filepath, 'wb+')
f.write(urllib.urlopen(url).read())
f.close()
except:
print('> CANNOT DOWNLOAD PICTURE %s' % url)
return
# CV stuff
print(' - cv module starts')
test_img = cv2.imread(filepath, 0)
rp = RawPhoto(test_img, num_papers, num_questions)
res = rp.dump_data()
rp.paper_objs = []
print(' - cv module done')
print(res)
# clean up
remove(filepath)
# send header
self.send_response(200)
self.send_header("Content-type", "text/json")
self.end_headers()
self.wfile.write(res)
self.wfile.close()
if __name__ == '__main__':
httpd = SocketServer.TCPServer(("", PORT), RequestHandler)
httpd.serve_forever()