-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
67 lines (59 loc) · 2.62 KB
/
server.py
File metadata and controls
67 lines (59 loc) · 2.62 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
60
61
62
63
64
65
66
67
## https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface
from flask import Flask, request, send_file
import json
app = Flask(__name__)
# 'Are you alive?'
@app.route('/')
def AYA():
docs = 'https://pythonbasics.org/flask-rest-api/'
return f'Go for <a href ="{docs}">docu\'drama<a/>...'
# GET request for an app (with a HTML-based GUI) - or whathever you decide w.r.t. the value of <converter> parameter
@app.route('/apps/<whatever>', methods = ['GET'])
def download_file(whatever):
return send_file('./form.html', as_attachment = False)
# A GET request for an image (with...
@app.route('/imgs/SFP.JPEG', methods = ['GET'])
def sweet_image():
return send_file('./San Francisco Peaks.JPEG', as_attachment = False)
# ... or without a syntactic sugar)
def sugar_free_image():
return send_file('./Saguaro Park.JPEG', as_attachment = False)
app.add_url_rule('/imgs/SP.JPEG', 'sugar_free_image', sugar_free_image, methods = ['GET'])
# A GET request for an image (with...
@app.route('/imgs/ally.png', methods = ['GET'])
def sweet_ally():
return send_file('./ally.png', as_attachment = False)
# GET request form (a baby RPC)
@app.route('/converter')
def edge_form():
value = request.args.get('MPG')
mpg2lkm = 3.78541178/1.609344 * 100 #gallon/mile * km
return 'Gotcha {:.3}l/100km!'.format(mpg2lkm/eval(value))
# POST & JSON web service request (a big daddy RPC)
@app.route('/Freddy0b10Jason', methods = ['POST'])
def Freddy0b10Jason():
args = request.json
print('RAW request', request, args)
# Sometimes servers gotta do what the servers gotta do...
args['ID'], args['severity'], args['message'] = 0o52, 2.718281828459045, '¡Python On Rails!'
# https://stackoverflow.com/questions/37237034/how-to-get-results-out-of-a-python-exec-eval-call
command, aux = args['code'], {}
try:
exec(command, aux)
args['code'] = str(aux['result'])
except KeyError:
args['code'] = "Use 'result' variable to get (yes, you guessed it right) the result!"
except:
args['code'] = 'No result. What. So.. Ever...'
print('Reply-to a client: '), print(json.dumps(args, indent = 0b11, ensure_ascii = False).encode('utf8').decode())
# ... and send a result back to a client
return args
'''Run a server and wait...
and wait...
and wait...
... for clients' requests to arrive...
http://localhost:8006/apps/whatever-floats-your-boat or http://localhost:8006/converter?MPG=52
http://localhost:8006/imgs/SP.JPEG or http://localhost:8006/imgs/SFP.JPEG
'''
if __name__ == '__main__':
app.run(host = 'localhost', port = '8006', debug = True)