Serve client connections with async handlers.
Section: Streams
Start a TCP server
python
python
import asyncio
async def handle(reader, writer):
data = await reader.readline()
writer.write(b"echo: " + data)
await writer.drain()
writer.close()
await writer.wait_closed()
async def main():
server = await asyncio.start_server(handle, "127.0.0.1", 9000)
async with server:
await server.serve_forever()
asyncio.run(main())Explanation
Use `start_server()` for lightweight TCP services.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Streams
Run blocking I/O in a thread
Use `asyncio.to_thread()` for simple thread offloading.
Run a shell command asynchronously
Spawn a shell pipeline with `create_subprocess_shell()`.