README.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/discourse/status?server=https%3A%2F%2Faio-libs.discourse.group
  22. :target: https://aio-libs.discourse.group
  23. :alt: Discourse status
  24. .. image:: https://badges.gitter.im/Join%20Chat.svg
  25. :target: https://gitter.im/aio-libs/Lobby
  26. :alt: Chat on Gitter
  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 middlewares and plugable 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. loop = asyncio.get_event_loop()
  49. loop.run_until_complete(main())
  50. This prints:
  51. .. code-block::
  52. Status: 200
  53. Content-type: text/html; charset=utf-8
  54. Body: <!doctype html> ...
  55. Coming from `requests <https://requests.readthedocs.io/>`_ ? Read `why we need so many lines <https://aiohttp.readthedocs.io/en/latest/http_request_lifecycle.html>`_.
  56. Server
  57. ------
  58. An example using a simple server:
  59. .. code-block:: python
  60. # examples/server_simple.py
  61. from aiohttp import web
  62. async def handle(request):
  63. name = request.match_info.get('name', "Anonymous")
  64. text = "Hello, " + name
  65. return web.Response(text=text)
  66. async def wshandle(request):
  67. ws = web.WebSocketResponse()
  68. await ws.prepare(request)
  69. async for msg in ws:
  70. if msg.type == web.WSMsgType.text:
  71. await ws.send_str("Hello, {}".format(msg.data))
  72. elif msg.type == web.WSMsgType.binary:
  73. await ws.send_bytes(msg.data)
  74. elif msg.type == web.WSMsgType.close:
  75. break
  76. return ws
  77. app = web.Application()
  78. app.add_routes([web.get('/', handle),
  79. web.get('/echo', wshandle),
  80. web.get('/{name}', handle)])
  81. if __name__ == '__main__':
  82. web.run_app(app)
  83. Documentation
  84. =============
  85. https://aiohttp.readthedocs.io/
  86. Demos
  87. =====
  88. https://github.com/aio-libs/aiohttp-demos
  89. External links
  90. ==============
  91. * `Third party libraries
  92. <http://aiohttp.readthedocs.io/en/latest/third_party.html>`_
  93. * `Built with aiohttp
  94. <http://aiohttp.readthedocs.io/en/latest/built_with.html>`_
  95. * `Powered by aiohttp
  96. <http://aiohttp.readthedocs.io/en/latest/powered_by.html>`_
  97. Feel free to make a Pull Request for adding your link to these pages!
  98. Communication channels
  99. ======================
  100. *aio-libs discourse group*: https://aio-libs.discourse.group
  101. *gitter chat* https://gitter.im/aio-libs/Lobby
  102. We support `Stack Overflow
  103. <https://stackoverflow.com/questions/tagged/aiohttp>`_.
  104. Please add *aiohttp* tag to your question there.
  105. Requirements
  106. ============
  107. - Python >= 3.6
  108. - async-timeout_
  109. - attrs_
  110. - charset-normalizer_
  111. - multidict_
  112. - yarl_
  113. Optionally you may install the cChardet_ and aiodns_ libraries (highly
  114. recommended for sake of speed).
  115. .. _charset-normalizer: https://pypi.org/project/charset-normalizer
  116. .. _aiodns: https://pypi.python.org/pypi/aiodns
  117. .. _attrs: https://github.com/python-attrs/attrs
  118. .. _multidict: https://pypi.python.org/pypi/multidict
  119. .. _yarl: https://pypi.python.org/pypi/yarl
  120. .. _async-timeout: https://pypi.python.org/pypi/async_timeout
  121. .. _cChardet: https://pypi.python.org/pypi/cchardet
  122. License
  123. =======
  124. ``aiohttp`` is offered under the Apache 2 license.
  125. Keepsafe
  126. ========
  127. The aiohttp community would like to thank Keepsafe
  128. (https://www.getkeepsafe.com) for its support in the early days of
  129. the project.
  130. Source code
  131. ===========
  132. The latest developer version is available in a GitHub repository:
  133. https://github.com/aio-libs/aiohttp
  134. Benchmarks
  135. ==========
  136. If you are interested in efficiency, the AsyncIO community maintains a
  137. list of benchmarks on the official wiki:
  138. https://github.com/python/asyncio/wiki/Benchmarks