asyncio: Transports Move Bytes, Protocols Decide Which Bytes

asyncio Transports are the "how" (moving bytes), while Protocols are the "what" (deciding which bytes to send). They're the low-level foundation for libraries handling raw socket I/O.
Why it exists
Transports and Protocols exist to provide a high-performance, callback-based abstraction over raw network and inter-process communication (IPC). They allow library authors to build efficient protocols, like HTTP or a database driver, without managing non-blocking sockets and the event loop manually, while still achieving maximum control.
The mental model
A Transport is the delivery mechanism; it's concerned with how bytes are transmitted over a channel like TCP or a subprocess pipe. A Protocol is the application logic; it determines which bytes to send and how to interpret incoming bytes. There is always a one-to-one relationship: the Protocol calls transport.write() to send data, and the Transport calls the Protocol's data_received() method with incoming data.
How it works
You don't instantiate these classes directly. Instead, you use a low-level event loop method like loop.create_connection(), passing it a protocol_factory (a callable that returns a Protocol instance). The event loop creates the appropriate Transport and links it to your Protocol. Communication is callback-driven. For example, when data arrives on the socket, the event loop calls the transport, which in turn calls your protocol's data_received(data) method. This is fundamentally different from the async/await style of high-level stream APIs.
When to use it
Only when building a networking library or framework from the ground up. If you were implementing a client for a protocol like Redis (RESP) or a custom RPC system, these primitives would offer the necessary performance and control over the byte stream and connection lifecycle.
When not to use it
In almost all application code. Using Transports and Protocols directly is a common footgun. For standard TCP communication, use asyncio.open_connection(), which provides a StreamReader and StreamWriter for use with async/await. For HTTP, use a dedicated library like httpx or aiohttp. These higher-level APIs are safer, easier to reason about, and built upon transports and protocols for you.
One canonical example
A simple TCP echo server. Its Protocol class would define a connection_made(transport) method to store the transport, and a data_received(data) method that simply calls self.transport.write(data). The server itself would be started with loop.create_server(), passing it a factory that creates instances of this echo protocol for each new client connection.
Interview question
When is it appropriate for a developer to directly implement asyncio Transports and Protocols?
- a.When building a new networking library for a custom byte-stream protocol.Correct
- b.When developing a high-performance HTTP client for a web service.
- c.When implementing a basic TCP server for a typical application.
- d.When optimizing asynchronous file input/output operations.
Why? this is the answer
The card explicitly states that Transports and Protocols should be used "Only when building a networking library or framework from the ground up" for custom protocols. For common tasks like HTTP clients or basic TCP servers, higher-level APIs are recommended, as direct use is considered a "common footgun" for application code.
Just read this? Test yourself on what you have been reading.
Read the original → docs.python.org
- #python
- #asyncio
- #networking
- #low-level-api
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles