Protocols are asyncio’s primitives supplied as convenient base classes to quickly set up clients or servers using TCP/UDP (+ subprocesses). These are especially helpful when we need to implement low level handling of protocol of some sort. I believe they are inspired by Twisted’s protcols.

Simple example of TCP echo server protocol may be (taken from asyncio docs):

  1. First thing is to inherit from asyncio-provided base class asyncio.Protocol. It serves for handling TCP and SSL based communication.
  2. Implementing given protocol comes down to writing particular callbacks, starting from connection_made which is invoked everytime we have new connection from the outside.
  3. data_received is called when client sends some data to the server
  4. connection_lost is invoked when connection ends either from client side or server.

It all seems nice and clean, but where are coroutines? Where is async and await? Well, they are not there. If one needs to introduce them, then they should be decoupled using asyncio.ensure_future: