liveBook · Manning

Socket module

As explained in Python Concurrency with asyncio

The Python socket module is a standard-library module that provides interfaces for creating and manipulating network sockets.

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.

How Python Concurrency with asyncio covers it

How deep it goes
Explains socket creation, options, binding, listening, and acceptance through a runnable server, stopping before message exchange.
Examples
Uses a localhost server on port 8000 that accepts one client and prints its address.
What you can do after
Build a basic TCP listener using socket(), setsockopt(), bind(), listen(), and accept().
How it connects
Connects network sockets with client-server architecture, TCP, UDP, and blocking I/O.

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.