0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-15 15:53:51 +01:00
synapse/demo/webserver.py

60 lines
1.5 KiB
Python
Raw Normal View History

2014-08-12 16:10:52 +02:00
import argparse
import BaseHTTPServer
import os
import SimpleHTTPServer
import cgi, logging
2014-08-12 16:10:52 +02:00
from daemonize import Daemonize
2019-06-20 11:32:02 +02:00
class SimpleHTTPRequestHandlerWithPOST(SimpleHTTPServer.SimpleHTTPRequestHandler):
UPLOAD_PATH = "upload"
"""
Accept all post request as file upload
"""
2019-06-20 11:32:02 +02:00
def do_POST(self):
path = os.path.join(self.UPLOAD_PATH, os.path.basename(self.path))
2019-06-20 11:32:02 +02:00
length = self.headers["content-length"]
data = self.rfile.read(int(length))
2019-06-20 11:32:02 +02:00
with open(path, "wb") as fh:
fh.write(data)
self.send_response(200)
2019-06-20 11:32:02 +02:00
self.send_header("Content-Type", "application/json")
self.end_headers()
# Return the absolute path of the uploaded file
self.wfile.write('{"url":"/%s"}' % path)
2014-08-12 16:10:52 +02:00
def setup():
parser = argparse.ArgumentParser()
parser.add_argument("directory")
parser.add_argument("-p", "--port", dest="port", type=int, default=8080)
2019-06-20 11:32:02 +02:00
parser.add_argument("-P", "--pid-file", dest="pid", default="web.pid")
2014-08-12 16:10:52 +02:00
args = parser.parse_args()
# Get absolute path to directory to serve, as daemonize changes to '/'
os.chdir(args.directory)
dr = os.getcwd()
2019-06-20 11:32:02 +02:00
httpd = BaseHTTPServer.HTTPServer(("", args.port), SimpleHTTPRequestHandlerWithPOST)
2014-08-12 16:10:52 +02:00
def run():
os.chdir(dr)
httpd.serve_forever()
daemon = Daemonize(
2019-06-20 11:32:02 +02:00
app="synapse-webclient", pid=args.pid, action=run, auto_close_fds=False
)
2014-08-12 16:10:52 +02:00
daemon.start()
2019-06-20 11:32:02 +02:00
if __name__ == "__main__":
2014-08-12 16:10:52 +02:00
setup()