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.
Shows the mailbox-to-post-office model: one listening socket accepts clients and assigns each a separate socket for communication. section 3.1
