import textwrap from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import unquote COW = r""" \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || """ def cowsay(text, width=40): lines = textwrap.wrap(text, width) or [""] w = max(len(line) for line in lines) out = [" " + "_" * (w + 2)] if len(lines) == 1: out.append(f"< {lines[0]} >") else: for i, line in enumerate(lines): line = line.ljust(w) if i == 0: out.append(f"/ {line} \\") elif i == len(lines) - 1: out.append(f"\\ {line} /") else: out.append(f"| {line} |") out.append(" " + "-" * (w + 2)) return "\n".join(out) + "\n" + COW class Handler(BaseHTTPRequestHandler): def do_GET(self): path = self.path.split("?", 1)[0].lstrip("/") if path == "favicon.ico": self.send_response(404) self.end_headers() return # "+" and "_" become spaces, then %xx decoding (so %2B still gives a literal +) text = unquote(path.replace("+", " ").replace("_", " "))[:500] or "moo" body = cowsay(text).encode() self.send_response(200) self.send_header("Content-Type", "text/plain; charset=utf-8") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def log_message(self, *args): pass ThreadingHTTPServer(("0.0.0.0", 8080), Handler).serve_forever()