Skip to content

Instantly share code, notes, and snippets.

@DuqueDeTuring
Created November 17, 2022 11:32
Show Gist options
  • Save DuqueDeTuring/ea7023f56416ba619190725709f3d13d to your computer and use it in GitHub Desktop.
Save DuqueDeTuring/ea7023f56416ba619190725709f3d13d to your computer and use it in GitHub Desktop.
small http server, receives an html string and returns it converted into text (without: tags, links, tables, images, etc)
from http.server import BaseHTTPRequestHandler, HTTPServer
import html2text
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
try:
data = self.rfile.read(int(self.headers['Content-Length'])).decode("utf-8")
content = h.handle(data.strip()).strip()
print(content)
self.send_response(200)
except Exception as exc:
print(exc)
self.send_response(500)
content = "ERROR converter"
self.send_header("Content-Length", str(len(content)))
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(bytes(content, "utf-8"))
h = html2text.HTML2Text()
h.ignore_links = True
h.bypass_tables = True
h.ignore_images = True
h.ignore_anchors = True
h.ignore_emphasis = True
with HTTPServer(('', 8007), Handler) as server:
server.serve_forever()
@DuqueDeTuring
Copy link
Author

DuqueDeTuring commented Nov 17, 2022

exploring using it as a little service to convert Mastodon's API html content to plain text and showing the result on a small LCD screen connected to a Raspberry Pi Pico W.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment