-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
52 lines (43 loc) · 1.52 KB
/
api.py
File metadata and controls
52 lines (43 loc) · 1.52 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
#!/usr/bin/env python
import bottle
import subprocess
import os
import classifier
import settings
import utils
from custom import MissingFile
p1 = subprocess.Popen(['ip','addr','show','eth0'],stdout=subprocess.PIPE)
p2 = subprocess.Popen(['sed','-rn',r's/\s*inet\s(([0-9]{1,3}\.){3}[0-9]{1,3}).*/\1/p'],stdin=p1.stdout,stdout=subprocess.PIPE)
p1.stdout.close()
ip_addr = p2.communicate()[0].strip()
p1.wait()
app = bottle.app()
@app.hook('after_request')
def handle_cors():
"""
Let there be no cors at all ;)
"""
bottle.response.headers['Access-Control-Allow-Origin'] = '*'
bottle.response.headers['Access-Control-Allow-Methods'] = 'PUT, GET, POST, DELETE, OPTIONS'
bottle.response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
@bottle.route('/')
def index():
return {'status': 'ok'}
@bottle.route('/classifications', method=['OPTIONS', 'POST'])
def classify():
if bottle.request.method == 'OPTIONS':
return {}
try:
img = utils.save_image(bottle.request)
return classifier.classify(settings.UPLOADS + img.filename)
except MissingFile:
return {'error': 'missing image file'}
@bottle.route('/urls', method=['OPTIONS', 'POST'])
def classify_url():
if bottle.request.method == 'OPTIONS':
return {}
filename= utils.save_image_from_url(bottle.request)
return classifier.classify(settings.UPLOADS + filename)
if __name__=='__main__':
bottle.debug(True)
bottle.run(app=app, host='localhost', port=80)