The Python socket module provides standard-library interfaces for creating, reading, writing, and manipulating network sockets. A server socket listens for clients; after accepting one, the server uses a separate connection socket to communicate with that client. section 3.1
A basic TCP server follows this sequence:
- Create an IPv4 TCP socket with socket.AF_INET and socket.SOCK_STREAM. section 3.1
- Enable SO_REUSEADDR, bind the socket to a hostname and port, and begin listening. section 3.1
- Call accept() to receive a client connection and its address. section 3.1
Listening on localhost
A minimal server creates a TCP socket, binds it to 127.0.0.1:8000, listens, accepts one client, and prints the client address.
