ASGI (Asynchronous Server Gateway Interface) is a standard interface between web servers and Python web applications. It is the spiritual successor to WSGI (Web Server Gateway Interface) but is designed to support asynchronous capabilities using Python's `async` and `await` syntax. Unlike WSGI, which is synchronous and primarily focused on handling HTTP requests, ASGI is capable of handling various communication protocols, including HTTP, WebSockets, and long-polling, making it suitable for modern, real-time web applications. An ASGI application is essentially an `async` callable that takes three arguments: `scope`, `receive`, and `send`.
An ASGI Server is a program that implements the ASGI specification on the server side. Its primary role is to listen for incoming network requests (like HTTP requests or WebSocket connections), translate these raw requests into a standardized Python dictionary format (the `scope` and `event` messages), and pass them to an ASGI application. It then takes the responses generated by the ASGI application and sends them back over the network to the client. Examples of ASGI servers include uvicorn, Hypercorn, and Daphne.
Uvicorn is a widely used, high-performance ASGI server implementation. It is known for its speed and efficiency, largely due to its foundation on `asyncio` (Python's built-in asynchronous I/O framework) and `uvloop` (a faster, drop-in replacement for the default `asyncio` event loop). Uvicorn also leverages `httptools` for efficient HTTP parsing and `websockets` for WebSocket handling. It is the recommended server for popular ASGI frameworks like FastAPI and Starlette.
How 'ASGI Server + uvicorn' works: In practice, when you develop an asynchronous Python web application (e.g., using FastAPI or Starlette, or even a raw ASGI callable), `uvicorn` acts as the specific ASGI server that runs your application. You point `uvicorn` to your ASGI application, and it takes care of the low-level network communication, translating client requests into ASGI-compliant `scope` and `event` messages for your application, and then transmitting your application's `send` messages back to the client. This combination provides a robust and performant environment for deploying asynchronous Python web services.
Example Code
1. Save the following code as 'main.py':
main.py
async def application(scope, receive, send):
Assert that this is an HTTP request scope
assert scope['type'] == 'http'
The 'scope' is a dictionary containing details about the request (e.g., method, path, headers)
'receive' is an awaitable callable that yields incoming messages (e.g., request body chunks)
'send' is an awaitable callable that sends outgoing messages (e.g., response headers, body chunks)
For simple GET requests, you might not need to await receive() immediately
For POST/PUT requests, you'd await receive() to get the request body
if scope['path'] == '/hello':
response_body = b'Hello, World!'
status_code = 200
elif scope['path'] == '/':
response_body = b'Welcome to the ASGI server example!'
status_code = 200
else:
response_body = b'Not Found'
status_code = 404
Send the HTTP response start event (headers)
await send({
'type': 'http.response.start',
'status': status_code,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()]
],
})
Send the HTTP response body event
await send({
'type': 'http.response.body',
'body': response_body,
'more_body': False Indicate that this is the last part of the body
})
2. Install uvicorn (if you haven't already):
pip install uvicorn
3. Run the application using uvicorn from your terminal in the same directory as main.py:
uvicorn main:application --reload
Explanation of the command:
- 'uvicorn': The command to run the uvicorn server.
- 'main': Refers to the Python file 'main.py'.
- 'application': Refers to the 'application' callable (function) defined inside 'main.py'.
- '--reload': (Optional) Enables auto-reloading of the server when code changes are detected.
4. Access the application in your web browser or with a tool like curl:
- http://127.0.0.1:8000/hello
- http://127.0.0.1:8000/
- http://127.0.0.1:8000/anything_else (will return 'Not Found')








ASGI Server + uvicorn