Network Programming with sockets
Published by Brayan K
A socket is an endpoint for sending and receiving bytes between programs over a network, and Python's socket module lets you build both the client and the server sides of that conversation.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A socket is an endpoint; TCP is a stream of bytes
A TCP socket transports an ordered byte stream. It does not preserve the boundaries between your send calls as application messages. A receiver may obtain part of one message, several messages together, or the remaining part of a previous message. Designing a protocol therefore requires a framing rule as well as a connection.
The real socket examples here create and configure unconnected endpoints, then close them. They do not contact a server, bind a listening port or use the network. The framing exercises use explicitly labelled test doubles with predetermined byte chunks. This lets you test protocol logic reliably; it does not test real transport behavior, connection establishment or operating-system timeouts.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as endpoint:
print(endpoint.family == socket.AF_INET)
print(endpoint.type == socket.SOCK_STREAM)
endpoint.settimeout(2.0)
print(endpoint.gettimeout())
print(endpoint.fileno() == -1)
# ✅ Expected output:
# True
# True
# 2.0
# TrueAF_INET selects the IPv4 address family and SOCK_STREAM selects stream semantics. Creating the socket does not connect it. A client normally connects an endpoint; a server binds a listening socket, listens and accepts. accept returns a separate connected socket for that conversation. The listening socket continues to accept other connections.
For real applications, name resolution, connection errors, authentication and encryption are additional responsibilities. Plain TCP does not encrypt or authenticate application data. Higher-level HTTP clients are covered in REST API Clients & External Service Integrations; do not recreate an HTTP or TLS implementation merely to fetch a web resource.
A receive limit is a maximum, not a complete message request
class ChunkSource:
"""A deterministic test double, not a network connection."""
def __init__(self, chunks):
self.chunks = list(chunks)
def recv(self, limit):
if not self.chunks:
return b""
chunk = self.chunks.pop(0)
result, remaining = chunk[:limit], chunk[limit:]
if remaining:
self.chunks.insert(0, remaining)
return result
source = ChunkSource([b"he", b"llo"])
print(source.recv(100))
print(source.recv(100))
print(source.recv(100))
# ✅ Expected output:
# b'he'
# b'llo'
# b''This test double returns its configured fragments and then an empty bytes object. On a connected stream, a positive-size recv returning b"" indicates end-of-stream from the peer’s sending side. It does not mean “try again because no data has arrived yet.” A temporarily unavailable nonblocking read instead raises BlockingIOError; a timed-out operation raises a timeout exception.
Never use recv(0) to test whether a peer closed: requesting zero bytes does not establish that fact. Also distinguish end-of-stream from a protocol-level empty message; a length-prefixed protocol can legitimately contain a zero-length payload.
class ChunkSource:
"""A deterministic test double, not a network connection."""
def __init__(self, chunks):
self.chunks = list(chunks)
def recv(self, limit):
if not self.chunks:
return b""
chunk = self.chunks.pop(0)
result, remaining = chunk[:limit], chunk[limit:]
if remaining:
self.chunks.insert(0, remaining)
return result
def read_exact(recv, count):
if count < 0:
raise ValueError("negative byte count")
data = bytearray()
while len(data) < count:
part = recv(count - len(data))
if not part:
raise EOFError("stream ended before frame completed")
data.extend(part)
return bytes(data)
source = ChunkSource([b"h", b"ell", b"o!"])
print(read_exact(source.recv, 5).decode("ascii"))
print(read_exact(source.recv, 1).decode("ascii"))
# ✅ Expected output:
# hello
# !The loop requests only the missing byte count, preserving later bytes for the next operation. Its progress condition is the accumulated length. An empty chunk before that count is reached is a truncated frame, not a successful shorter message. The caller still needs a timeout or deadline policy for a real peer that never sends enough data.
class ChunkSource:
"""A deterministic test double, not a network connection."""
def __init__(self, chunks):
self.chunks = list(chunks)
def recv(self, limit):
if not self.chunks:
return b""
chunk = self.chunks.pop(0)
result, remaining = chunk[:limit], chunk[limit:]
if remaining:
self.chunks.insert(0, remaining)
return result
def read_exact(recv, count):
if count < 0:
raise ValueError("negative byte count")
data = bytearray()
while len(data) < count:
part = recv(count - len(data))
if not part:
raise EOFError("stream ended before frame completed")
data.extend(part)
return bytes(data)
source = ChunkSource([b"hi"])
read_exact(source.recv, 3)
# ✅ Expected error:
# EOFError: stream ended before frame completedChoose framing before decoding text
fragment = "é".encode("utf-8")[:1]
fragment.decode("utf-8")
# ✅ Expected error:
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: unexpected end of dataA byte chunk is not necessarily a complete UTF-8 character sequence. Reassemble the payload before decoding, or deliberately use an incremental decoder for a streaming text protocol. Strings, Bytes & Unicode Encodings owns encoding errors and incremental decoding; here the protocol decides which bytes belong together.
class ChunkSource:
"""A deterministic test double, not a network connection."""
def __init__(self, chunks):
self.chunks = list(chunks)
def recv(self, limit):
if not self.chunks:
return b""
chunk = self.chunks.pop(0)
result, remaining = chunk[:limit], chunk[limit:]
if remaining:
self.chunks.insert(0, remaining)
return result
def read_exact(recv, count):
if count < 0:
raise ValueError("negative byte count")
data = bytearray()
while len(data) < count:
part = recv(count - len(data))
if not part:
raise EOFError("stream ended before frame completed")
data.extend(part)
return bytes(data)
payload = "café".encode("utf-8")
frame = len(payload).to_bytes(2, "big") + payload
source = ChunkSource([frame[:1], frame[1:4], frame[4:]])
size = int.from_bytes(read_exact(source.recv, 2), "big")
if size > 64:
raise ValueError("frame exceeds 64-byte limit")
message = read_exact(source.recv, size).decode("utf-8")
print(size)
print(message)
# ✅ Expected output:
# 5
# caféThe two-byte header counts encoded bytes, not characters. Both peers must agree on header size, byte order, maximum payload size and encoding. Reject an oversized length before allocating or waiting for that payload. A representable length is not automatically an acceptable application size. Binary Data with struct owns packing structured binary fields.
header = (65).to_bytes(2, "big")
size = int.from_bytes(header, "big")
if size > 64:
raise ValueError("frame exceeds 64-byte limit")
# ✅ Expected error:
# ValueError: frame exceeds 64-byte limitchunks = [b"alpha\nbe", b"ta\ngamma\n"]
buffer = bytearray()
messages = []
for chunk in chunks:
buffer.extend(chunk)
while b"\n" in buffer:
line, _, remaining = buffer.partition(b"\n")
messages.append(bytes(line).decode("utf-8"))
buffer = bytearray(remaining)
print(messages)
print(len(buffer))
# ✅ Expected output:
# ['alpha', 'beta', 'gamma']
# 0A delimiter protocol needs rules for delimiters inside payloads and a maximum buffered length. This controlled fixture contains short newline-delimited messages; it is not a production parser for arbitrary input. Do not discard the unfinished tail between reads, and do not assume one receive call contains only one line.
A successful send can still be a partial send
class SmallSink:
"""A deterministic partial-write test double."""
def __init__(self):
self.data = bytearray()
def send(self, data):
count = min(2, len(data))
self.data.extend(data[:count])
return count
sink = SmallSink()
payload = b"hello"
offset = 0
while offset < len(payload):
sent = sink.send(payload[offset:])
if sent == 0:
raise ConnectionError("send made no progress")
offset += sent
print(bytes(sink.data).decode("ascii"))
print(offset)
# ✅ Expected output:
# hello
# 5send returns the count accepted by that call. Advance by that count; resending the full original payload would duplicate bytes. For ordinary blocking socket code, sendall performs the repeated-send work and returns None on success. If it raises, it does not tell you exactly how much was sent, so retrying an entire non-idempotent request can duplicate an operation.
Even successful sendall does not prove the peer application processed the request. If that matters, define an application-level acknowledgement and request identifiers. Socket delivery and business-operation completion are different contracts.
Timeout, nonblocking mode and shutdown answer different questions
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as endpoint:
endpoint.setblocking(False)
print(endpoint.gettimeout())
endpoint.setblocking(True)
print(endpoint.gettimeout() is None)
endpoint.settimeout(1.5)
print(endpoint.gettimeout())
# ✅ Expected output:
# 0.0
# True
# 1.5import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as endpoint:
endpoint.settimeout(-1)
# ✅ Expected error:
# ValueError: Timeout value out of rangeNone means blocking mode, zero means nonblocking mode, and a positive timeout bounds waiting for socket operations. A loop that performs multiple operations may need a separate overall deadline; setting a per-operation timeout does not automatically bound a whole multi-message conversation. The examples print configured values, not elapsed timings.
shutdown(SHUT_WR) ends your sending direction while allowing receipt of remaining peer data. close releases your socket resource. A peer receiving end-of-stream can therefore still have data to send back. Decide when the protocol is finished, then close every owned connection, including the socket returned by accept. Context-manager lifetime mechanics are covered in contextlib: contextmanager, suppress, ExitStack.
Complete the short-read loop
Fill the missing byte count and the EOF test. The supplied test double fragments a four-byte payload; the loop must assemble it without asking for bytes beyond that payload.
class ChunkSource:
"""A deterministic test double, not a network connection."""
def __init__(self, chunks):
self.chunks = list(chunks)
def recv(self, limit):
if not self.chunks:
return b""
chunk = self.chunks.pop(0)
result, remaining = chunk[:limit], chunk[limit:]
if remaining:
self.chunks.insert(0, remaining)
return result
source = ChunkSource([b"A", b"BC", b"D"])
wanted = 4
data = bytearray()
while len(data) < wanted:
part = source.recv(___)
if ___:
raise EOFError("incomplete payload")
data.extend(part)
print(bytes(data).decode("ascii"))
# 👉 replace ___ with wanted - len(data)
# 👉 replace ___ with not part
# ✅ Expected output:
# ABCDRead two bounded messages from awkward fragments
Use a two-byte big-endian length followed by UTF-8 payload bytes. Encode the messages cat and café, concatenate their frames, and split the bytes at awkward positions using the provided ChunkSource idea. Read exactly two messages, enforce a 64-byte payload limit before reading each body, then confirm no unread bytes remain. This brief tests framing with a deterministic stream double; it does not open a network connection.
class ChunkSource:
"""A deterministic test double, not a network connection."""
def __init__(self, chunks):
self.chunks = list(chunks)
def recv(self, limit):
if not self.chunks:
return b""
chunk = self.chunks.pop(0)
result, remaining = chunk[:limit], chunk[limit:]
if remaining:
self.chunks.insert(0, remaining)
return result
def read_exact(recv, count):
if count < 0:
raise ValueError("negative byte count")
data = bytearray()
while len(data) < count:
part = recv(count - len(data))
if not part:
raise EOFError("stream ended before frame completed")
data.extend(part)
return bytes(data)
def encode_message(text):
payload = text.encode("utf-8")
if len(payload) > 64:
raise ValueError("frame exceeds 64-byte limit")
return len(payload).to_bytes(2, "big") + payload
wire = encode_message("cat") + encode_message("café")
source = ChunkSource([wire[:1], wire[1:4], wire[4:8], wire[8:]])
messages = []
for _ in range(2):
size = int.from_bytes(read_exact(source.recv, 2), "big")
if size > 64:
raise ValueError("frame exceeds 64-byte limit")
messages.append(read_exact(source.recv, size).decode("utf-8"))
print(messages)
print(source.recv(1) == b"")
# ✅ Expected output:
# ['cat', 'café']
# True
Practice quiz
A connected TCP socket returns two bytes from recv(100). What does that imply?
- Two bytes are available in this result; the protocol may need more
- The peer violated TCP
- The entire message must be two bytes
- The remaining 98 bytes were lost
Answer: Two bytes are available in this result; the protocol may need more. The requested size is a maximum, not an application-message length.
What does this program print? payload = "é".encode("utf-8") print(len(payload))
- 1
- 2
- 0
- 4
Answer: 2. A length header for byte transport counts encoded bytes rather than text characters.
A positive-size recv returns b"". Which interpretation is correct for a connected stream?
- Retry immediately forever
- No bytes happened to be ready yet
- The peer sending direction reached end-of-stream
- A timeout was raised
Answer: The peer sending direction reached end-of-stream. Temporary unavailability and timeouts have different signals; an empty result indicates EOF.
What does this program print? data = b"abcdef" sent = 2 print(data[sent:])
- b'ab'
- b'abcdef'
- b'cd'
- b'cdef'
Answer: b'cdef'. Advance past the accepted prefix before sending the remaining bytes.
What does this program print? header = (5).to_bytes(2, "big") print(int.from_bytes(header, "big"))
- 5
- 2
- 256
- 0
Answer: 5. The agreed byte order reconstructs the encoded length.
Why must a declared frame length be checked before reading the body?
- To make TCP preserve messages
- To enforce resource and protocol limits
- To turn bytes into strings automatically
- To guarantee the peer is authenticated
Answer: To enforce resource and protocol limits. A peer-controlled length must not cause unlimited allocation or waiting.
What does this program print? buffer = b"one two" line, _, rest = buffer.partition(b" ") print(line) print(rest)
- b'one two' b''
- b'two' b'one'
- b'one' b'two'
- b'one' b''
Answer: b'one' b'two'. Partition separates one complete delimiter-terminated message and retains its tail.
What does sendall returning successfully establish?
- The peer application committed the operation
- A response has already arrived
- The request can never be duplicated
- The bytes were sent without that call raising an error
Answer: The bytes were sent without that call raising an error. Application processing requires a separate acknowledgement contract.
Continue this course
- Previous: High-Level File Operations (shutil)
- Next: Checkpoint: Concurrency & I/O — Combine threads, queues, temp files and sockets in a build — then a quiz
- Quick reference: Python cheat sheet