Fundamentals 13 min read

Understanding Port Listening with Python Socket Examples

This article uses a conversational story to explain what it means to listen on a network port, covering TCP vs UDP, binding to specific IPs, reuse‑port options, IPv4 and IPv6 differences, and demonstrates the concepts with multiple Python socket code snippets.

Architecture Digest
Architecture Digest
Architecture Digest
Understanding Port Listening with Python Socket Examples

The piece begins with a dialogue between two students, Liz and Tim, who discuss the meaning of listening on a port while working on a networking assignment.

Tim learns that a listening socket allows other processes to connect to the bound port, and that the operating system maintains a table mapping ports, IP addresses, protocols, and socket descriptors.

Several Python examples illustrate the concepts:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 8080))
sock.listen()
print(sock.accept())

This TCP server binds to 127.0.0.1:8080 and accepts a connection.

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('127.0.0.1', 8080))
print(sock.recv(1024))

This UDP server shows that without calling listen() the port does not accept connections.

Liz demonstrates that multiple processes can listen on the same port using the SO_REUSEPORT socket option:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('127.0.0.1', 8080))
sock.listen()
print(sock.accept())

They explore binding to different IP addresses (127.0.0.2, 127.0.0.3) and to the wildcard address 0.0.0.0, noting that the latter listens on all local IPv4 interfaces.

The discussion then moves to IPv6, showing how to bind to the IPv6 wildcard address '::' and how the IPV6_V6ONLY option can separate IPv6 and IPv4 listeners:

import socket
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
sock.bind(('::', 8080))
sock.listen()
print(sock.accept())

Using netcat and lsof , they observe which processes are listening and how connections succeed or fail depending on protocol and address bindings.

The overall takeaway is that listening on a port involves a combination of port number, IP address, protocol (TCP/UDP), and IP version, and that the OS maintains a hash map of these tuples to the corresponding sockets.

IPv6PythonTCPIPv4Socket ProgrammingUDPport listening
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.