Courses/Systems Concepts (read-only)

๐Ÿง  Systems Concepts (read-only)

Tiny TCP echo server

Accept connections and echo back.

A complete echo server in a dozen lines. In production reach for asyncio or socketserver.ThreadingTCPServer, but knowing the raw shape demystifies them.

import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
    srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    srv.bind(("127.0.0.1", 9000))
    srv.listen()
    print("listening on 9000")
    while True:
        conn, addr = srv.accept()
        with conn:
            data = conn.recv(1024)
            if not data: continue
            conn.sendall(b"echo: " + data)

**Next steps**

  • Many clients โ†’ selectors.DefaultSelector (level-triggered, single thread).
  • Async โ†’ asyncio.start_server, StreamReader / StreamWriter.
  • HTTP โ€” don't roll your own; use http.server for demos and a real framework otherwise.

> ๐Ÿงช Concept-only โ€” Pyodide can't bind ports.

Concept lesson

This topic relies on OS features the in-browser Python sandbox can't run (threads, subprocesses, sockets). Read the examples here, then try them in real CPython on your machine.

Sign in to track your progress across lessons.