aiohttp README

The project's README from github.com/aio-libs/aiohttp. As one markdown file: README.md.

================================== Async http client/server framework

.. image:: https://raw.githubusercontent.com/aio-libs/aiohttp/master/docs/aiohttp-plain.svg :height: 64px :width: 64px :alt: aiohttp logo

|

.. image:: https://github.com/aio-libs/aiohttp/workflows/CI/badge.svg :target: https://github.com/aio-libs/aiohttp/actions?query=workflow%3ACI :alt: GitHub Actions status for master branch

.. image:: https://codecov.io/gh/aio-libs/aiohttp/branch/master/graph/badge.svg :target: https://codecov.io/gh/aio-libs/aiohttp :alt: codecov.io status for master branch

.. image:: https://badge.fury.io/py/aiohttp.svg :target: https://pypi.org/project/aiohttp :alt: Latest PyPI package version

.. image:: https://img.shields.io/pypi/dm/aiohttp :target: https://pypistats.org/packages/aiohttp :alt: Downloads count

.. image:: https://readthedocs.org/projects/aiohttp/badge/?version=latest :target: https://docs.aiohttp.org/ :alt: Latest Read The Docs

.. image:: https://img.shields.io/endpoint?url=https://codspeed.io/badge.json :target: https://codspeed.io/aio-libs/aiohttp :alt: Codspeed.io status for aiohttp

Key Features

Getting started

Client

To get something from the web:

.. code-block:: python

import aiohttp import asyncio

async def main():

  async with aiohttp.ClientSession() as session:
      async with session.get('https://python.org') as response:

          print("Status:", response.status)
          print("Content-type:", response.headers['content-type'])

          html = await response.text()
          print("Body:", html[:15], "...")

asyncio.run(main())

This prints:

.. code-block::

Status: 200
Content-type: text/html; charset=utf-8
Body: <!doctype html> ...

Coming from requests <https://requests.readthedocs.io/>_ ? Read why we need so many lines <https://aiohttp.readthedocs.io/en/latest/http_request_lifecycle.html>_.

Server

An example using a simple server:

.. code-block:: python

# examples/server_simple.py
from aiohttp import web

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

async def wshandle(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async for msg in ws:
        if msg.type == web.WSMsgType.text:
            await ws.send_str("Hello, {}".format(msg.data))
        elif msg.type == web.WSMsgType.binary:
            await ws.send_bytes(msg.data)
        elif msg.type == web.WSMsgType.close:
            break

    return ws


app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/echo', wshandle),
                web.get('/{name}', handle)])

if __name__ == '__main__':
    web.run_app(app)

Documentation

https://aiohttp.readthedocs.io/

Demos

https://github.com/aio-libs/aiohttp-demos

External links

Feel free to make a Pull Request for adding your link to these pages!

Communication channels

aio-libs Discussions: https://github.com/aio-libs/aiohttp/discussions

Matrix: #aio-libs:matrix.org <https://matrix.to/#/#aio-libs:matrix.org>_

We support Stack Overflow <https://stackoverflow.com/questions/tagged/aiohttp>_. Please add aiohttp tag to your question there.

Requirements

Optionally you may install the aiodns_ library (highly recommended for sake of speed).

.. _aiodns: https://pypi.python.org/pypi/aiodns .. _multidict: https://pypi.python.org/pypi/multidict .. _frozenlist: https://pypi.org/project/frozenlist/ .. _yarl: https://pypi.python.org/pypi/yarl

Keepsafe

The aiohttp community would like to thank Keepsafe (https://www.getkeepsafe.com) for its support in the early days of the project.

Source code

The latest developer version is available in a GitHub repository: https://github.com/aio-libs/aiohttp

Benchmarks

If you are interested in efficiency, the AsyncIO community maintains a list of benchmarks on the official wiki: https://github.com/python/asyncio/wiki/Benchmarks


.. image:: https://img.shields.io/matrix/aio-libs:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat :target: https://matrix.to/#/%23aio-libs:matrix.org :alt: Matrix Room — #aio-libs:matrix.org

.. image:: https://img.shields.io/matrix/aio-libs-space:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs-space%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat :target: https://matrix.to/#/%23aio-libs-space:matrix.org :alt: Matrix Space — #aio-libs-space:matrix.org

.. image:: https://insights.linuxfoundation.org/api/badge/health-score?project=aiohttp :target: https://insights.linuxfoundation.org/project/aiohttp :alt: LFX Health Score

aiohttp (aio-libs/aiohttp)

Searching Database…

16.6K ·GitHub
What is this?aiohttp is an asynchronous HTTP client and server framework built on asyncio: one Python package for making HTTP requests and for building web applications, with WebSockets supported out of the box.
Is it a library I import into my code, or a framework that runs my whole app?Both ship in one package: as a client it is a library you call (aiohttp.ClientSession); as a server it is a framework — you write handlers, add routes to a web.Application and run it with web.run_app.
How do I make my first request with it?Install from PyPI (pip install aiohttp), then work inside an async function: open a ClientSession, call session.get(url) as a response and await response.text() for the body.
What does it depend on besides Python itself?Three required packages: multidict, yarl and frozenlist. Optionally add aiodns — the README calls it highly recommended for speed.
I already use requests — is switching painful?aiohttp code is asynchronous, so the same job takes more lines than with requests — the README even links a 'why we need so many lines' explainer for people coming from requests.
Can I build a web server with it, not just call other APIs?Yes — handlers are plain async functions: register them on a web.Application with routes (path segments like /{name} work) and start it all with web.run_app(app). Middleware and pluggable routing are built in.
Does it handle WebSockets on both ends?Yes — client and server WebSocket support is out of the box, the README's server example is literally an echo WebSocket handler, and the async API is advertised as avoiding callback hell.
Do I need to know asyncio before touching it?Yes — aiohttp is built for asyncio, so you write async def coroutines with await and kick them off with asyncio.run(main()), exactly as the getting-started example does.
Is it actually fast?The README publishes no numbers of its own — it just points readers interested in efficiency to the benchmark list the AsyncIO community maintains on the official wiki.
What are similar projects?starlette, Perfect, uvicorn, guzzle, actix-web, martini, ktor, koa
Creator
aio-libs
Size
44.8 MB
Stars
16.6K
Age
13y
Commits · 90 Days