Skip to main content

WSGI adapter for aiohttp.

Project description

aiohttp-wsgi
============

**aiohttp-wsgi** is a WSGI adapter for aiohttp.


Features
--------

- Run WSGI applications (e.g. Django, Flask) on `aiohttp <http://aiohttp.readthedocs.org>`_.
- Handle thousands of client connections, using the latest `evented networking library <https://docs.python.org/3.4/library/asyncio.html>`_.
- Add `websockets <http://aiohttp.readthedocs.org/en/v0.14.4/web.html#websockets>`_ to your
existing Python app!


Installation
------------

1. Install using ``pip install aiohttp-wsgi``.


Usage
-----


``middleware.wsgi(application, **kwargs)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`Middleware factory <http://aiohttp.readthedocs.org/en/v0.14.4/web.html#middlewares>`_ that wraps a WSGI application for use inside an aiohttp `Application <http://aiohttp.readthedocs.org/en/v0.14.4/web_reference.html#aiohttp.web.Application>`_.

::

from aiohttp.web import Application
from aiohttp_wsgi.middleware import wsgi
from your_app.wsgi import application

aiohttp_application = Application(middlewares=[
wsgi(application),
])


**Available arguments:**

``script_name``
The URL prefix to mount the WSGI application. Corresponds to ``environ["SCRIPT_NAME"]``. This should **not** end with a slash. Defaults to ``""``.

``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 to ``sys.stderr``.

``executor``
An `Executor <https://docs.python.org/dev/library/concurrent.futures.html#executor-objects>`_ instance used to run WSGI requests. Defaults to the asyncio base executor.

``loop``
The asyncio `loop <https://docs.python.org/3.4/library/asyncio-eventloop.html#base-event-loop>`_. Defaults to ``asyncio.get_event_loop()``.


``configure_server(application, **kwargs)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

High-level factory method that wraps a WSGI application in an asyncio `server <https://docs.python.org/3.4/library/asyncio-eventloop.html#server>`_ and aiohttp `Application <http://aiohttp.readthedocs.org/en/v0.14.4/web_reference.html#aiohttp.web.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``.

``routes``
A list of ``(method, path, handler)`` routes to add to the aiohttp `Application <http://aiohttp.readthedocs.org/en/v0.14.4/web_reference.html#aiohttp.web.Application>`_. Defaults to ``[]``.

``static``
A list of ``(path, dirname)`` static routes to add to the aiohttp `Application <http://aiohttp.readthedocs.org/en/v0.14.4/web_reference.html#aiohttp.web.Application>`_. Defaults to ``[]``.


``configure_server()`` also accepts all arguments available to ``middleware.wsgi()``.



``serve(application, **kwargs)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

High-level factory method that starts a `server <https://docs.python.org/3.4/library/asyncio-eventloop.html#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()``.


Design
------

WSGI applications are run on an asyncio `executor <https://docs.python.org/3.4/library/asyncio-eventloop.html#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 <http://aiohttp.readthedocs.org/en/v0.14.4/web.html#websockets>`_)
can be run on the same network port, using the `aiohttp router <http://aiohttp.readthedocs.org/en/v0.14.4/web.html#run-a-simple-web-server>`_
to switch between your WSGI app and asyncronous code.


Build status
------------

This project is built on every push using the Travis-CI service.

.. image:: https://travis-ci.org/etianen/aiohttp-wsgi.svg?branch=master
:target: https://travis-ci.org/etianen/aiohttp-wsgi


Support and announcements
-------------------------

Downloads and bug tracking can be found at the `main project
website <http://github.com/etianen/aiohttp-wsgi>`_.


More information
----------------

The aiohttp-wsgi project was developed by Dave Hall. You can get the code
from the `aiohttp-wsgi project site <http://github.com/etianen/aiohttp-wsgi>`_.

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:

- `Website <http://www.etianen.com/>`_
- `Twitter <http://twitter.com/etianen>`_
- `Google Profile <http://www.google.com/profiles/david.etianen>`_

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-wsgi-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

File details

Details for the file aiohttp-wsgi-0.1.1.tar.gz.

File metadata

File hashes

Hashes for aiohttp-wsgi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 512d4ce8b69c9f3443d4ff1be9a9c27031941048f6a89e1cca0213bdcc828802
MD5 7fc5ebc63b23d825cbdae4c11236b748
BLAKE2b-256 26cb2e57b189448142c1adba19395ecfc067ffa1d10e93f46a2fa4c46782cd7e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page