liveBook · Manning

Socket.accept

As explained in Python Concurrency with asyncio

socket.accept is a socket method that accepts an incoming connection and returns a connected socket together with the client's address.

socket.accept waits for an incoming client connection on a listening server socket, then returns a new connected socket and the client's address. The listening socket remains available to accept additional clients. section 3.1 section 3.2.2

A typical server uses socket.accept after preparing its listening socket:

  • Create and configure a socket, bind it to a hostname and port, and call listen(). section 3.1
  • Call connection, client_address = server_socket.accept() to receive a client-specific communication socket. section 3.1
  • Repeat accept in a loop to collect multiple connections, while recognizing that blocking accept and recv calls can stall other clients. section 3.2.2

Minimal connection acceptor

server_socket.listen()\nconnection, client_address = server_socket.accept()\nprint(f'I got a connection from {client_address}!')\n\nUse connection for subsequent recv or sendall operations; use server_socket to accept another client. section 3.1 section 3.2.1

Figure 3.1 A client connects to our server socket. The server then creates a new socket to communicate with the client.
03-01

Shows the mailbox-to-post-office model: one listening socket accepts clients and assigns each a separate socket for communication. section 3.1

How Python Concurrency with asyncio covers it

How deep it goes
Defines returned values and repeated use, then exposes blocking behavior without implementing non-blocking handling.
Examples
Demonstrates a localhost server, repeated client acceptance, Telnet connections, buffering, and an echo server.
What it assumes
Assumes basic socket concepts while establishing server, client, listening, and per-client communication roles.
What you can do after
Apply it via Python's socket module to accept clients and route later reads and writes through connected sockets.
How it connects
Connects server sockets with listening, TCP, client addresses, recv, sendall, and non-blocking sockets.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.