1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#!/usr/bin/env python
"""
Simple Test/Demo Server for running bleach.clean output on various
desktops.
Usage:
python server.py
"""
import http.server
import socketserver
import bleach
PORT = 8080
class BleachCleanHandler(http.server.SimpleHTTPRequestHandler):
# Prevent 'cannot bind to address' errors on restart
allow_reuse_address = True
def do_POST(self):
content_len = int(self.headers.get("content-length", 0))
body = self.rfile.read(content_len)
print("read {} bytes: {}".format(content_len, body))
body = body.decode("utf-8")
print("input: %r" % body)
cleaned = bleach.clean(body)
self.send_response(200)
self.send_header("Content-Length", len(cleaned))
self.send_header("Content-Type", "text/plain;charset=UTF-8")
self.end_headers()
cleaned = bytes(cleaned, encoding="utf-8")
print("cleaned: %r" % cleaned)
self.wfile.write(cleaned)
if __name__ == "__main__":
httpd = socketserver.TCPServer(("127.0.0.1", PORT), BleachCleanHandler)
print("listening on localhost port %d" % PORT)
httpd.serve_forever()
|