Newer
Older
import argparse
import BaseHTTPServer
import os
import SimpleHTTPServer

Emmanuel ROHEE
committed
import cgi, logging

Emmanuel ROHEE
committed
class SimpleHTTPRequestHandlerWithPOST(SimpleHTTPServer.SimpleHTTPRequestHandler):
UPLOAD_PATH = "upload"
"""
Accept all post request as file upload
"""

Emmanuel ROHEE
committed
def do_POST(self):
path = os.path.join(self.UPLOAD_PATH, os.path.basename(self.path))

Emmanuel ROHEE
committed
data = self.rfile.read(int(length))

Emmanuel ROHEE
committed
fh.write(data)
self.send_response(200)

Emmanuel ROHEE
committed
self.end_headers()
# Return the absolute path of the uploaded file
self.wfile.write('{"url":"/%s"}' % path)
def setup():
parser = argparse.ArgumentParser()
parser.add_argument("directory")
parser.add_argument("-p", "--port", dest="port", type=int, default=8080)
parser.add_argument("-P", "--pid-file", dest="pid", default="web.pid")
args = parser.parse_args()
# Get absolute path to directory to serve, as daemonize changes to '/'
os.chdir(args.directory)
dr = os.getcwd()
httpd = BaseHTTPServer.HTTPServer(("", args.port), SimpleHTTPRequestHandlerWithPOST)
def run():
os.chdir(dr)
httpd.serve_forever()
daemon = Daemonize(
app="synapse-webclient", pid=args.pid, action=run, auto_close_fds=False
)