Running a WSGI app

aiohttp_wsgi allows you to run WSGI applications (e.g. Django, Flask) on aiohttp. This allows you to add async features like websockets and long-polling to an existing Python web app.

Hint

If you don’t need to add websockets or async request handlers to your app, but still want to run your WSGI app on the asyncio event loop, aiohttp_wsgi provides a simpler command line interface.

Run a web server

In order to implement a WSGI server, first import your WSGI application and wrap it in a WSGIHandler.

from aiohttp import web
from aiohttp_wsgi import WSGIHandler
from your_project.wsgi import application

wsgi_handler = WSGIHandler(application)

Next, create an Application instance and register the request handler with the application’s router on a particular HTTP method and path:

app = web.Application()
app.router.add_route("*", "/{path_info:.*}", wsgi_handler)

After that, run the application by run_app() call:

web.run_app(app)

See the aiohttp.web documentation for information on adding websockets and async request handlers to your app.

Serving simple WSGI apps

If you don’t need to add websockets or async request handlers to your app, but still want to run your WSGI app on the asyncio event loop, aiohttp_wsgi provides a simple serve() helper.

from aiohttp_wsgi import serve

serve(application)

Extra environ keys

aiohttp_wsgi adds the following additional keys to the WSGI environ:

asyncio.executor
The Executor running the WSGI request.
aiohttp.request
The raw aiohttp.web.Request that initiated the WSGI request. Use this to access additional request metadata.

API reference

class aiohttp_wsgi.WSGIHandler(application: Callable[[Dict[str, Any], Callable[[str, List[Tuple[str, str]]], Callable[bytes, None]]], Iterable[bytes]], *, url_scheme: Optional[str] = None, stderr: Optional[IO[bytes]] = None, inbuf_overflow: int = 524288, max_request_body_size: int = 1073741824, executor: Optional[concurrent.futures._base.Executor] = None)

An adapter for WSGI applications, allowing them to run on aiohttp.

Parameters:
  • application – A WSGI application callable.
  • url_scheme (str) – A hint about the URL scheme used to access the application. Corresponds to environ['wsgi.url_scheme']. Default is auto-detected to 'http' or 'https'.
  • stderr (io.BytesIO) – A file-like value for WSGI error logging. Corresponds to environ['wsgi.errors']. Defaults to sys.stderr.
  • inbuf_overflow (int) – A tempfile will be created if the request body is larger than this value, which is measured in bytes. Defaults to 524288.
  • max_request_body_size (int) – Maximum number of bytes in request body. Defaults to 1073741824. Larger requests will receive a HTTP 413 (Request Entity Too Large) response.
  • executor (concurrent.futures.Executor) – An Executor instance used to run WSGI requests. Defaults to the asyncio base executor.
aiohttp_wsgi.serve(application: Callable[[Dict[str, Any], Callable[[str, List[Tuple[str, str]]], Callable[bytes, None]]], Iterable[bytes]], **kwargs) → None

Runs the WSGI application on aiohttp, serving it until keyboard interrupt.

Parameters:
  • application – A WSGI application callable.
  • url_scheme (str) – A hint about the URL scheme used to access the application. Corresponds to environ['wsgi.url_scheme']. Default is auto-detected to 'http' or 'https'.
  • stderr (io.BytesIO) – A file-like value for WSGI error logging. Corresponds to environ['wsgi.errors']. Defaults to sys.stderr.
  • inbuf_overflow (int) – A tempfile will be created if the request body is larger than this value, which is measured in bytes. Defaults to 524288.
  • max_request_body_size (int) – Maximum number of bytes in request body. Defaults to 1073741824. Larger requests will receive a HTTP 413 (Request Entity Too Large) response.
  • threads (int) – Number of threads used to process application logic. Defaults to 4.
  • host (str) – Host interfaces to bind. Defaults to '0.0.0.0' and '::'.
  • port (int) – Port to bind. Defaults to 8080.
  • unix_socket (str) – Path to a unix socket to bind, cannot be used with host.
  • unix_socket_perms (int) – Filesystem permissions to apply to the unix socket. Defaults to 384.
  • backlog (int) – Socket connection backlog. Defaults to 1024.
  • static (list) – Static root mappings in the form (path, directory). Defaults to ()
  • static_cors (list) – Set to ‘*’ to enable CORS on static files for all origins, or a string to enable CORS for a specific origin. Defaults to None
  • script_name (str) – URL prefix for the WSGI application, should start with a slash, but not end with a slash. Defaults to ''.
  • shutdown_timeout (int) – Timeout when closing client connections on server shutdown. Defaults to 60.0.