Skip to content

Instantly share code, notes, and snippets.

@noamtamim
Created June 10, 2025 13:17
Show Gist options
  • Save noamtamim/cd098e1d4ccb7f00832312c1e7d40de7 to your computer and use it in GitHub Desktop.
Save noamtamim/cd098e1d4ccb7f00832312c1e7d40de7 to your computer and use it in GitHub Desktop.
Server that returns whatever you ask it to
import wsgiref
import threading
@asynccontextmanager
async def http_server(headers: dict, body: bytes):
def app(_, start_response):
start_response("200 OK", list(headers.items()))
return [body] if body else []
server = wsgiref.simple_server.make_server("localhost", 0, app)
threading.Thread(target=server.serve_forever).start()
# Wait for the server to start
await asyncio.sleep(0.1)
try:
yield f"http://localhost:{server.server_port}"
finally:
server.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment