An asyncio.Protocol is a Python class that receives callbacks for network-connection events. It owns connection lifecycle behavior, while a transport sends and receives the underlying data. section 8.1 section 8.2
A protocol commonly responds to these transport events:
- connection_made receives a transport after the socket connects and can immediately send a request. section 8.2
- data_received receives response bytes and may run repeatedly, so the protocol must buffer incomplete data. section 8.2
- eof_received signals the end of the socket response, helping determine when processing is complete. section 8.2
A callback-based HTTP client
Subclass asyncio.Protocol, store a Future and response buffer, send an HTTP GET from connection_made, collect chunks in data_received, and return the completed response through an asynchronous get_response method. A protocol factory supplies the instance to loop.create_connection. section 8.2
Figure 8.2 A transport calls methods on a protocol when events happen. A protocol can write data to a transport.
The transport–protocol interaction diagram shows transports invoking protocol callbacks and protocols writing data back to transports. section 8.2
