Created
March 28, 2025 13:43
-
-
Save noamtamim/34c38666484fb8f8404f853736eaf2db to your computer and use it in GitHub Desktop.
Smallest possible Python web server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This server returns an empty JSON object, no matter what. | |
from wsgiref.simple_server import make_server | |
import threading | |
def start_server(): | |
def app(environ, start_response): | |
start_response('200 OK', [('Content-Type', 'application/json')]) | |
return [b'{}'] | |
with make_server('', 8000, app) as httpd: | |
httpd.serve_forever() | |
# Option 1: just run. Control C to stop. | |
start_server() | |
# Option 2: start in a background thread (will exit with main thread). | |
threading.Thread(target=start_server, daemon=True).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment