File: server.py

package info (click to toggle)
python-bleach 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,348 kB
  • sloc: python: 14,628; sh: 60; makefile: 51
file content (48 lines) | stat: -rwxr-xr-x 1,157 bytes parent folder | download
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()