README.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ==================================
  2. Async http client/server framework
  3. ==================================
  4. .. image:: https://raw.githubusercontent.com/aio-libs/aiohttp/master/docs/aiohttp-plain.svg
  5. :height: 64px
  6. :width: 64px
  7. :alt: aiohttp logo
  8. |
  9. .. image:: https://github.com/aio-libs/aiohttp/workflows/CI/badge.svg
  10. :target: https://github.com/aio-libs/aiohttp/actions?query=workflow%3ACI
  11. :alt: GitHub Actions status for master branch
  12. .. image:: https://codecov.io/gh/aio-libs/aiohttp/branch/master/graph/badge.svg
  13. :target: https://codecov.io/gh/aio-libs/aiohttp
  14. :alt: codecov.io status for master branch
  15. .. image:: https://badge.fury.io/py/aiohttp.svg
  16. :target: https://pypi.org/project/aiohttp
  17. :alt: Latest PyPI package version
  18. .. image:: https://readthedocs.org/projects/aiohttp/badge/?version=latest
  19. :target: https://docs.aiohttp.org/
  20. :alt: Latest Read The Docs
  21. .. 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
  22. :target: https://matrix.to/#/%23aio-libs:matrix.org
  23. :alt: Matrix Room — #aio-libs:matrix.org
  24. .. 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
  25. :target: https://matrix.to/#/%23aio-libs-space:matrix.org
  26. :alt: Matrix Space — #aio-libs-space:matrix.org
  27. Key Features
  28. ============
  29. - Supports both client and server side of HTTP protocol.
  30. - Supports both client and server Web-Sockets out-of-the-box and avoids
  31. Callback Hell.
  32. - Provides Web-server with middleware and pluggable routing.
  33. Getting started
  34. ===============
  35. Client
  36. ------
  37. To get something from the web:
  38. .. code-block:: python
  39. import aiohttp
  40. import asyncio
  41. async def main():
  42. async with aiohttp.ClientSession() as session:
  43. async with session.get('http://python.org') as response:
  44. print("Status:", response.status)
  45. print("Content-type:", response.headers['content-type'])
  46. html = await response.text()
  47. print("Body:", html[:15], "...")
  48. asyncio.run(main())
  49. This prints:
  50. .. code-block::
  51. Status: 200
  52. Content-type: text/html; charset=utf-8
  53. Body: <!doctype html> ...
  54. Coming from `requests <https://requests.readthedocs.io/>`_ ? Read `why we need so many lines <https://aiohttp.readthedocs.io/en/latest/http_request_lifecycle.html>`_.
  55. Server
  56. ------
  57. An example using a simple server:
  58. .. code-block:: python
  59. # examples/server_simple.py
  60. from aiohttp import web
  61. async def handle(request):
  62. name = request.match_info.get('name', "Anonymous")
  63. text = "Hello, " + name
  64. return web.Response(text=text)
  65. async def wshandle(request):
  66. ws = web.WebSocketResponse()
  67. await ws.prepare(request)
  68. async for msg in ws:
  69. if msg.type == web.WSMsgType.text:
  70. await ws.send_str("Hello, {}".format(msg.data))
  71. elif msg.type == web.WSMsgType.binary:
  72. await ws.send_bytes(msg.data)
  73. elif msg.type == web.WSMsgType.close:
  74. break
  75. return ws
  76. app = web.Application()
  77. app.add_routes([web.get('/', handle),
  78. web.get('/echo', wshandle),
  79. web.get('/{name}', handle)])
  80. if __name__ == '__main__':
  81. web.run_app(app)
  82. Documentation
  83. =============
  84. https://aiohttp.readthedocs.io/
  85. Demos
  86. =====
  87. https://github.com/aio-libs/aiohttp-demos
  88. External links
  89. ==============
  90. * `Third party libraries
  91. <http://aiohttp.readthedocs.io/en/latest/third_party.html>`_
  92. * `Built with aiohttp
  93. <http://aiohttp.readthedocs.io/en/latest/built_with.html>`_
  94. * `Powered by aiohttp
  95. <http://aiohttp.readthedocs.io/en/latest/powered_by.html>`_
  96. Feel free to make a Pull Request for adding your link to these pages!
  97. Communication channels
  98. ======================
  99. *aio-libs Discussions*: https://github.com/aio-libs/aiohttp/discussions
  100. *gitter chat* https://gitter.im/aio-libs/Lobby
  101. We support `Stack Overflow
  102. <https://stackoverflow.com/questions/tagged/aiohttp>`_.
  103. Please add *aiohttp* tag to your question there.
  104. Requirements
  105. ============
  106. - async-timeout_
  107. - attrs_
  108. - multidict_
  109. - yarl_
  110. - frozenlist_
  111. Optionally you may install the aiodns_ library (highly recommended for sake of speed).
  112. .. _aiodns: https://pypi.python.org/pypi/aiodns
  113. .. _attrs: https://github.com/python-attrs/attrs
  114. .. _multidict: https://pypi.python.org/pypi/multidict
  115. .. _frozenlist: https://pypi.org/project/frozenlist/
  116. .. _yarl: https://pypi.python.org/pypi/yarl
  117. .. _async-timeout: https://pypi.python.org/pypi/async_timeout
  118. License
  119. =======
  120. ``aiohttp`` is offered under the Apache 2 license.
  121. Keepsafe
  122. ========
  123. The aiohttp community would like to thank Keepsafe
  124. (https://www.getkeepsafe.com) for its support in the early days of
  125. the project.
  126. Source code
  127. ===========
  128. The latest developer version is available in a GitHub repository:
  129. https://github.com/aio-libs/aiohttp
  130. Benchmarks
  131. ==========
  132. If you are interested in efficiency, the AsyncIO community maintains a
  133. list of benchmarks on the official wiki:
  134. https://github.com/python/asyncio/wiki/Benchmarks