Hypercorn

0.18.0 · active · verified Thu Apr 09

Hypercorn is an ASGI and WSGI web server based on Hyper libraries and inspired by Gunicorn. It supports HTTP/1, HTTP/2, WebSockets (over HTTP/1 and HTTP/2), ASGI/2, and ASGI/3 specifications, and can utilize asyncio, uvloop, or trio worker types. Currently at version 0.18.0, Hypercorn is actively maintained with regular updates and focuses on robust protocol support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically run a simple ASGI 'Hello, world!' application using Hypercorn's asyncio backend. It configures Hypercorn to bind to an address and port, defaulting to 0.0.0.0:8000, and uses `asyncio.run` to start the server. Alternatively, you can run from the command line: `hypercorn my_module:app` after installing.

import asyncio
from hypercorn.config import Config
from hypercorn.asyncio import serve

async def app(scope, receive, send):
    assert scope['type'] == 'http'
    response_body = b'Hello, world!'
    headers = [
        (b'content-type', b'text/plain'),
        (b'content-length', str(len(response_body)).encode())
    ]
    await send({'type': 'http.response.start', 'status': 200, 'headers': headers})
    await send({'type': 'http.response.body', 'body': response_body})

async def main():
    config = Config()
    config.bind = [f"0.0.0.0:{os.environ.get('PORT', '8000')}"]
    print(f"Serving on {config.bind[0]}...")
    await serve(app, config)

if __name__ == '__main__':
    import os
    asyncio.run(main())

view raw JSON →