aiohttp

PaweĊ‚ Fertyk (@pfertyk)



aiohttp.pfertyk.me

What is aiohttp?

  • HTTP client/server for asyncio (PEP 3156)
  • requests-like HTTP client
  • high level HTTP server interface meant for building web applications
  • low level HTTP server for web servers that don't need the conveniences of the high level server
  • supports for both Server and Client WebSockets out-of-the-box

Client

Session

  • get('http://api.com/events/')
  • post('http://api.com/events/', data=b'data')
  • put('http://api.com/events/1/', data=b'data')
  • delete('http://api.com/events/1/')
  • head('http://api.com/events/')
  • options('http://api.com/events/')
  • patch('http://api.com/events/1/', data=b'data')

Response

  • text()
  • text(encoding='windows-1251')
  • read()
  • json()
  • content.read(10)

Server

Router

  • add_get
  • add_post
  • add_patch
  • add_delete
  • add_route

WebSockets

  • reading from the WebSocket must only be done inside the request handler task
  • aiohttp.web creates an implicit asyncio.Task for handling every incoming request
  • aiohttp.web only supports WebSockets without downgrading to Long Polling (SockJS)
  • parallel reads from a WebSocket are forbidden

Low level server

  • web handler accepts BaseRequest
  • returns HTTP Response
  • handles StreamResponse
  • raises HTTPException
  • handles WebSocketResponse

Questions?