Created
June 10, 2025 13:17
-
-
Save noamtamim/cd098e1d4ccb7f00832312c1e7d40de7 to your computer and use it in GitHub Desktop.
Server that returns whatever you ask it to
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
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