ASGI (Asynchronous Server Gateway Interface) is a modern, asynchronous spiritual successor to WSGI, designed to provide a standard interface between asynchronous Python web servers, frameworks, and applications. Unlike WSGI, which is synchronous and primarily focused on HTTP/1.0 request/response cycles, ASGI supports full asynchronous capabilities, allowing for handling long-lived connections, WebSockets, and HTTP/2. It's built around a conceptual "scope," "receive," and "send" model, enabling applications to be protocol-agnostic and highly concurrent.
Hypercorn is a robust, production-ready ASGI server implementation written in Python. It's built upon `asyncio` and is capable of serving HTTP/1.1, HTTP/2, and WebSocket applications. Hypercorn is a popular choice for deploying ASGI frameworks like FastAPI, Starlette, Django Channels, and more. Key features include:
- Asynchronous I/O: Leverages Python's `asyncio` for high performance and concurrency.
- Protocol Support: Handles HTTP/1.1, HTTP/2, and WebSockets out of the box.
- Configuration: Offers extensive configuration options via CLI, environment variables, or a TOML file.
- Graceful Shutdown: Ensures ongoing requests are completed before shutting down worker processes.
- Process Management: Can be run with multiple worker processes to fully utilize CPU cores.
When you use Hypercorn with an ASGI application, Hypercorn acts as the bridge that translates incoming network requests (e.g., HTTP GET or WebSocket connection initiation) into the standardized ASGI interface calls that your application understands. Your application then processes these calls asynchronously and sends responses back through the ASGI interface, which Hypercorn then translates back into network responses. This combination allows Python web applications to achieve high concurrency and efficiently handle modern web protocols, making them suitable for real-time applications and APIs.
Example Code
File: app.py
import asyncio
async def simple_asgi_app(scope, receive, send):
"""
A simple ASGI application that responds with 'Hello, ASGI!' for HTTP requests.
It demonstrates the core 'scope', 'receive', 'send' interface.
"""
if scope['type'] == 'http':
Process an HTTP request
The 'receive' channel can be used to get the request body, but for a simple GET, it's often not needed.
await receive() In a real app, you might read request body data here
Send the HTTP response headers
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'text/plain'],
],
})
Send the HTTP response body
await send({
'type': 'http.response.body',
'body': b'Hello, ASGI from Hypercorn!',
})
elif scope['type'] == 'websocket':
Example for a WebSocket connection (requires more complex handling for actual communication)
await send({'type': 'websocket.accept'})
while True:
message = await receive()
if message['type'] == 'websocket.disconnect':
break
if message['type'] == 'websocket.receive' and 'text' in message:
await send({
'type': 'websocket.send',
'text': f"Echo: {message['text']}"
})
else:
Handle other scope types like 'lifespan' (for startup/shutdown events)
pass
To run this ASGI application with Hypercorn:
1. Ensure you have Hypercorn installed: `pip install hypercorn`
2. Save the code above as `app.py` in your project directory.
3. Open your terminal, navigate to the directory where `app.py` is saved.
4. Run the application using the Hypercorn command:
`hypercorn app:simple_asgi_app --bind 0.0.0.0:8000`
After running, you can access the HTTP endpoint by visiting `http://localhost:8000` in your web browser or using `curl http://localhost:8000`.
For WebSocket, you'd need a WebSocket client to connect to `ws://localhost:8000`.








ASGI Server + Hypercorn