Use Flask for Python environment
Flask gives us easy routing support, and simple interface for getting at request data.
This commit is contained in:
@@ -1,47 +1,32 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import imp
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
from flask import Flask
|
||||
from flask import request
|
||||
from flask import abort
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
codepath = '/userfunc/user'
|
||||
|
||||
userfunc = None
|
||||
|
||||
@app.route('/specialize', methods=['POST'])
|
||||
def load():
|
||||
global userfunc
|
||||
userfunc = (imp.load_source('user', codepath)).main
|
||||
return ""
|
||||
|
||||
@app.route('/', methods=['GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'DELETE'])
|
||||
def f():
|
||||
if userfunc == None:
|
||||
print("Generic container: no requests supported")
|
||||
abort(500)
|
||||
return userfunc()
|
||||
|
||||
#
|
||||
# Request context that's passed to user functions
|
||||
# TODO: this starts the built-in server, which isn't the most
|
||||
# efficient. We should use something better.
|
||||
#
|
||||
class Context:
|
||||
def __init__(self, handler):
|
||||
self.handler = handler
|
||||
|
||||
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
global userfunc
|
||||
userfunc = (imp.load_source('user', codepath)).main
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes("ok\n", "utf8"))
|
||||
return
|
||||
|
||||
# GET
|
||||
def do_GET(self):
|
||||
global userfunc
|
||||
try:
|
||||
print("GET request")
|
||||
ctx = Context(self)
|
||||
message = userfunc(ctx)
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes(message, "utf8"))
|
||||
return
|
||||
except:
|
||||
self.send_response(500)
|
||||
self.end_headers()
|
||||
|
||||
def run():
|
||||
print('starting server...')
|
||||
server_address = ('0.0.0.0', 8888)
|
||||
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
|
||||
httpd.serve_forever()
|
||||
|
||||
|
||||
run()
|
||||
app.run(host='0.0.0.0', port='8888')
|
||||
|
||||
Reference in New Issue
Block a user