socket.bind associates a server socket with a local hostname and port, giving clients a network address to connect to. section 3.1
Use bind as part of the server setup sequence:
- Create an IPv4 TCP socket with socket.AF_INET and socket.SOCK_STREAM. section 3.1
- Bind the socket to an address such as ('127.0.0.1', 8000). section 3.1
- Call listen, then accept connections arriving at the bound server socket. section 3.1
Bind a local TCP server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_address = ('127.0.0.1', 8000) server_socket.bind(server_address) server_socket.listen() section 3.1
Figure 3.1 A client connects to our server socket. The server then creates a new socket to communicate with the client.
Visualizes the server socket as a main mailbox that accepts a client and provides a separate communication socket. section 3.1
