aiohttp-wsgi is a WSGI adapter for aiohttp.
- Run WSGI applications (e.g. Django, Flask) on aiohttp.
- Handle thousands of client connections, using the latest evented networking library.
- Add websockets to your existing Python app!
- Install using
pip install aiohttp-wsgi
.
Handler that wraps a WSGI application for use inside an aiohttp Application.
from aiohttp.web import Application
from aiohttp_wsgi import WSGIHandler
from your_app.wsgi import application
aiohttp_application = Application()
wsgi_handler = WSGIHandler(application)
aiohttp_application.router.add_route("*", "/{path_info:.*}", wsgi_handler.handle_request)
Available arguments:
url_scheme
hint about the URL scheme used to access the application. Corresponds to
environ["wsgi.uri_scheme"]
. Default value auto-detected to"http"
or"https"
.stderr
A file-like value for WSGI error logging. Corresponds to
environ["wsgi.errors"]
. Defaults tosys.stderr
.inbuf_overflow
A tempfile will be created if the request body is larger than
inbuf_overflow
, which is measured in bytes. Defaults to 512K (524288).max_request_body_size
Maximum number of bytes in request body. Defaults to 1GB (1073741824). Larger requests will receive a HTTP 413 (Request Entity Too Large) response.
executor
An Executor instance used to run WSGI requests. Defaults to the asyncio base executor.
loop
The asyncio loop. Defaults to
asyncio.get_event_loop()
.
High-level factory method that wraps a WSGI application in an asyncio server and aiohttp Application.
from aiohttp_wsgi import configure_server
from your_app.wsgi import application
server, app = configure_server(application)
Available arguments:
host
The IP address to bind the server. Defaults to
"0.0.0.0"
.port
The network port to bind the server. Defaults to
8080
.unix_socket
The path to a unix socket to bind the server. Overrides
host
.unix_socket_perms
A set of filesystem permissions to apply to the unix socket. Defaults to
0o600
.socket
A preexisting socket object to use for the server. Overrides
host
.backlog
The maximum number of queued connections for the socket. Defaults to
1024
.routes
A list of
(method, path, handler)
routes to add to the aiohttp Application. Defaults to[]
.static
A list of
(path, dirname)
static routes to add to the aiohttp Application. Defaults to[]
.on_finish
A list of callbacks to be executed when the server shuts down. Each callback will be passed the aiohttp Application.
script_name
The URL prefix to mount the WSGI application. Corresponds to
environ["SCRIPT_NAME"]
. This should not end with a slash. Defaults to""
.
configure_server()
also accepts all arguments available to WSGIHandler()
.
High-level factory method that starts a server running a WSGI application.
from aiohttp_wsgi import serve
from your_app.wsgi import application
serve(application)
serve()
accepts all arguments available to configure_server()
.
WSGI applications are run on an asyncio executor. This allows existing Python frameworks like Django and Flask to run normally without blocking the main event loop or resorting to hacks like monkey-patching the Python standard library. This enables you to write the majority of your application code in a safe, predictable environment.
Asyncronous parts of your application (e.g. websockets) can be run on the same network port, using the aiohttp router to switch between your WSGI app and asyncronous code.
This project is built on every push using the Travis-CI service.
Downloads and bug tracking can be found at the main project website.
The aiohttp-wsgi project was developed by Dave Hall. You can get the code from the aiohttp-wsgi project site.
Dave Hall is a freelance web developer, based in Cambridge, UK. You can usually find him on the Internet in a number of different places: