From 8a9559be1fd1fa2a0b4cadbd79c0ad9bf330b32f Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Sun, 13 Nov 2016 23:38:37 -0800 Subject: [PATCH 1/2] Pass a valid context --- environments/python3/server.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/environments/python3/server.py b/environments/python3/server.py index 6f42b0ef..d030a89f 100644 --- a/environments/python3/server.py +++ b/environments/python3/server.py @@ -3,12 +3,16 @@ import imp from http.server import BaseHTTPRequestHandler, HTTPServer -# -# Load the file. -# codepath = '/userfunc/user' userfunc = None +# +# Request context that's passed to user functions +# +class Context: + def __init__(self, handler): + self.handler = handler + class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): def do_POST(self): global userfunc @@ -23,7 +27,8 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): global userfunc try: print("GET request") - message = userfunc(None) + ctx = Context(self) + message = userfunc(ctx) self.send_response(200) self.end_headers() self.wfile.write(bytes(message, "utf8")) From 640e65f35f7cbb288325e06ccf9a69fb80de4c89 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Sat, 19 Nov 2016 20:04:46 -0800 Subject: [PATCH 2/2] Use Flask for Python environment Flask gives us easy routing support, and simple interface for getting at request data. --- environments/python3/Dockerfile | 15 +++++-- environments/python3/requirements.txt | 1 + environments/python3/server.py | 61 ++++++++++----------------- examples/python/hello.py | 4 +- 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/environments/python3/Dockerfile b/environments/python3/Dockerfile index 3f6c9fc8..b04d09be 100644 --- a/environments/python3/Dockerfile +++ b/environments/python3/Dockerfile @@ -1,3 +1,12 @@ -FROM python:3-onbuild -CMD [ "python", "./server.py" ] -EXPOSE 8888 +FROM ubuntu:16.04 + +RUN apt-get update -y +RUN apt-get install -y python3 python3-pip python3-dev build-essential + +COPY . /app +WORKDIR /app +RUN pip3 install -r requirements.txt + +ENTRYPOINT ["python3"] +CMD ["server.py"] + diff --git a/environments/python3/requirements.txt b/environments/python3/requirements.txt index e69de29b..9f85444c 100644 --- a/environments/python3/requirements.txt +++ b/environments/python3/requirements.txt @@ -0,0 +1 @@ +Flask===0.11.1 diff --git a/environments/python3/server.py b/environments/python3/server.py index d030a89f..4faca621 100644 --- a/environments/python3/server.py +++ b/environments/python3/server.py @@ -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') diff --git a/examples/python/hello.py b/examples/python/hello.py index 31fe7bde..2c3a3088 100644 --- a/examples/python/hello.py +++ b/examples/python/hello.py @@ -1,2 +1,2 @@ -def main(context): - return "Hello, world!" +def main(): + return "Hello, world!\n"