diff options
Diffstat (limited to 'demo/webserver.py')
-rw-r--r-- | demo/webserver.py | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/demo/webserver.py b/demo/webserver.py index 78f3213540..875095c877 100644 --- a/demo/webserver.py +++ b/demo/webserver.py @@ -2,9 +2,32 @@ import argparse import BaseHTTPServer import os import SimpleHTTPServer +import cgi, logging from daemonize import Daemonize +class SimpleHTTPRequestHandlerWithPOST(SimpleHTTPServer.SimpleHTTPRequestHandler): + UPLOAD_PATH = "upload" + + """ + Accept all post request as file upload + """ + def do_POST(self): + + path = os.path.join(self.UPLOAD_PATH, os.path.basename(self.path)) + length = self.headers['content-length'] + data = self.rfile.read(int(length)) + + with open(path, 'wb') as fh: + fh.write(data) + + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + + # Return the absolute path of the uploaded file + self.wfile.write('{"url":"/%s"}' % path) + def setup(): parser = argparse.ArgumentParser() @@ -19,7 +42,7 @@ def setup(): httpd = BaseHTTPServer.HTTPServer( ('', args.port), - SimpleHTTPServer.SimpleHTTPRequestHandler + SimpleHTTPRequestHandlerWithPOST ) def run(): |