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
Open a TCP connection
Use stream reader and writer objects.
OpenIn sheetpythonsame section
Run a subprocess with exec
Capture stdout and stderr asynchronously.
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()`.
Use `run_in_executor()`
Offload work with explicit loop executor integration.