Skip to content

Instantly share code, notes, and snippets.

@andreif
Last active January 11, 2024 21:04
Show Gist options
  • Save andreif/439157471eabc54728cbd00eb32b4bff to your computer and use it in GitHub Desktop.
Save andreif/439157471eabc54728cbd00eb32b4bff to your computer and use it in GitHub Desktop.
import http.server
class Handler(http.server.BaseHTTPRequestHandler):
def __getattr__(self, item):
if item.startswith('do_'):
return self.default
return getattr(super(), item)
def default(self):
print(self.command, self.path)
print(self.headers)
self.protocol_version = 'HTTP/1.0'
self.send_response(200)
self.send_header('a', '1')
self.send_header('a', '2')
self.end_headers()
self.wfile.write(b'ok')
with http.server.ThreadingHTTPServer(('0.0.0.0', 8080), RequestHandlerClass=Handler) as _:
print(f"Serving on port {_.server_port}...")
try:
_.serve_forever()
except KeyboardInterrupt:
exit(0)
python -m http.server -d /tmp 8080
from wsgiref import simple_server
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'application/json; charset=utf-8')])
return [b'true']
simple_server.ServerHandler.server_software = ''
with simple_server.make_server(host='0.0.0.0', port=8080, app=app) as _:
print(f"Serving on port {_.server_port}...")
_.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment