Claude Code for socket: Python Low-Level Network I/O — Claude Skills 360 Blog
Blog / AI / Claude Code for socket: Python Low-Level Network I/O
AI

Claude Code for socket: Python Low-Level Network I/O

Published: September 2, 2028
Read time: 5 min read
By: Claude Skills 360

Python’s socket module provides low-level BSD socket network I/O. import socket. socket: s = socket.socket(family=AF_INET, type=SOCK_STREAM) — TCP socket; use SOCK_DGRAM for UDP. connect: s.connect((host, port)). bind: s.bind((host, port))host="" or "0.0.0.0" for all interfaces. listen: s.listen(backlog). accept: conn, addr = s.accept() — blocks until client connects. send/recv: s.send(data) → bytes sent (may be partial); s.sendall(data) → sends all or raises; s.recv(bufsize) → bytes (empty = closed). UDP: s.sendto(data, (host, port)) / data, addr = s.recvfrom(bufsize). setsockopt: s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) — allow port reuse; TCP_NODELAY disables Nagle. settimeout: s.settimeout(5.0) — raises socket.timeout on blocking ops; settimeout(0) = non-blocking. makefile: f = s.makefile("rb") — file-like wrapper. create_connection: socket.create_connection((host, port), timeout=10) — high-level convenience with getaddrinfo fallback. create_server (3.8+): socket.create_server((host, port), reuse_port=True). getaddrinfo: socket.getaddrinfo(host, port, AF_INET, SOCK_STREAM) → list of (family, type, proto, canonname, sockaddr). Context manager: with socket.socket() as s: — auto-closes. Claude Code generates TCP echo servers, protocol parsers, port scanners, and service health-check clients.

CLAUDE.md for socket

## socket Stack
- Stdlib: import socket
- TCP client: socket.create_connection((host, port), timeout=10)
- TCP server: s = socket.create_server(("", 8080), reuse_port=True)
- UDP:  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- Opts: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- Recv: data = recv_all(conn, n)  # loop until n bytes received

socket Network I/O Pipeline

# app/sockutil.py — TCP client/server, framing, UDP, DNS, health check
from __future__ import annotations

import socket
import struct
import threading
import time
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Any, Callable, Generator


# ─────────────────────────────────────────────────────────────────────────────
# 1. Connection helpers
# ─────────────────────────────────────────────────────────────────────────────

def tcp_connect(host: str, port: int, timeout: float = 10.0) -> socket.socket:
    """
    Open a TCP connection and return the socket.
    Raises socket.timeout or ConnectionRefusedError on failure.

    Example:
        with tcp_connect("httpbin.org", 80) as s:
            s.sendall(b"GET / HTTP/1.0\\r\\nHost: httpbin.org\\r\\n\\r\\n")
            print(recv_until(s, b"\\r\\n\\r\\n"))
    """
    s = socket.create_connection((host, port), timeout=timeout)
    s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    return s


@contextmanager
def tcp_client(host: str, port: int, timeout: float = 10.0) -> Generator[socket.socket, None, None]:
    """Context manager TCP client that auto-closes the socket."""
    s = tcp_connect(host, port, timeout=timeout)
    with s:
        yield s


def recv_exactly(s: socket.socket, n: int) -> bytes:
    """
    Receive exactly n bytes from socket s (loops on partial reads).
    Raises ConnectionError if the connection is closed early.

    Example:
        header = recv_exactly(conn, 4)
    """
    buf = bytearray()
    while len(buf) < n:
        chunk = s.recv(n - len(buf))
        if not chunk:
            raise ConnectionError(f"Connection closed after {len(buf)} of {n} bytes")
        buf.extend(chunk)
    return bytes(buf)


def recv_until(s: socket.socket, delimiter: bytes, max_bytes: int = 65536) -> bytes:
    """
    Receive bytes until delimiter is found; return data including delimiter.
    Raises ValueError if max_bytes exceeded.

    Example:
        response_header = recv_until(s, b"\\r\\n\\r\\n")
    """
    buf = bytearray()
    while True:
        if len(buf) > max_bytes:
            raise ValueError(f"recv_until: exceeded {max_bytes} bytes without finding delimiter")
        chunk = s.recv(1024)
        if not chunk:
            break
        buf.extend(chunk)
        pos = buf.find(delimiter)
        if pos != -1:
            return bytes(buf[: pos + len(delimiter)])
    return bytes(buf)


# ─────────────────────────────────────────────────────────────────────────────
# 2. Length-prefixed framing
# ─────────────────────────────────────────────────────────────────────────────

_FRAME_HEADER = struct.Struct(">I")   # 4-byte big-endian length


def send_frame(s: socket.socket, data: bytes) -> None:
    """
    Send data with a 4-byte length prefix.

    Example:
        send_frame(conn, json.dumps({"action": "ping"}).encode())
    """
    s.sendall(_FRAME_HEADER.pack(len(data)) + data)


def recv_frame(s: socket.socket) -> bytes:
    """
    Receive a length-prefixed frame.

    Example:
        data = recv_frame(conn)
        msg = json.loads(data)
    """
    header = recv_exactly(s, _FRAME_HEADER.size)
    (length,) = _FRAME_HEADER.unpack(header)
    return recv_exactly(s, length)


# ─────────────────────────────────────────────────────────────────────────────
# 3. TCP server
# ─────────────────────────────────────────────────────────────────────────────

@dataclass
class TCPServer:
    """
    Simple threaded TCP server. Dispatches each connection to a handler function.

    Example:
        def handler(conn, addr):
            data = recv_frame(conn)
            send_frame(conn, b"ACK:" + data)

        server = TCPServer("127.0.0.1", 9000, handler)
        server.start()
        # ... run client ...
        server.stop()
    """
    host:    str
    port:    int
    handler: Callable[[socket.socket, Any], None]
    _sock:   socket.socket | None = None
    _thread: threading.Thread | None = None
    _running: bool = False

    def start(self) -> "TCPServer":
        self._sock = socket.create_server((self.host, self.port), reuse_port=False)
        self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._sock.settimeout(0.5)
        self._running = True
        self._thread = threading.Thread(target=self._serve, daemon=True)
        self._thread.start()
        return self

    def _serve(self) -> None:
        while self._running and self._sock:
            try:
                conn, addr = self._sock.accept()
            except socket.timeout:
                continue
            except OSError:
                break
            t = threading.Thread(target=self._handle_one, args=(conn, addr), daemon=True)
            t.start()

    def _handle_one(self, conn: socket.socket, addr: Any) -> None:
        with conn:
            try:
                self.handler(conn, addr)
            except Exception:
                pass

    def stop(self) -> None:
        self._running = False
        if self._sock:
            self._sock.close()
        if self._thread:
            self._thread.join(timeout=2.0)


# ─────────────────────────────────────────────────────────────────────────────
# 4. UDP helpers
# ─────────────────────────────────────────────────────────────────────────────

def udp_send(host: str, port: int, data: bytes, timeout: float = 2.0) -> None:
    """
    Send a UDP datagram.

    Example:
        udp_send("localhost", 514, b"<14>test log message")
    """
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        s.settimeout(timeout)
        s.sendto(data, (host, port))


def udp_request(host: str, port: int, data: bytes, bufsize: int = 4096, timeout: float = 2.0) -> bytes:
    """
    Send a UDP datagram and receive a response.

    Example:
        response = udp_request("8.8.8.8", 53, dns_query_bytes)
    """
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        s.settimeout(timeout)
        s.sendto(data, (host, port))
        data, _ = s.recvfrom(bufsize)
        return data


# ─────────────────────────────────────────────────────────────────────────────
# 5. DNS and health-check utilities
# ─────────────────────────────────────────────────────────────────────────────

def resolve(host: str, port: int = 80, family: int = socket.AF_INET) -> list[str]:
    """
    Resolve a hostname to a list of IP addresses.

    Example:
        resolve("google.com")   # ['142.250.x.x', ...]
    """
    addrs = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM)
    return list({addr[4][0] for addr in addrs})


def is_port_open(host: str, port: int, timeout: float = 1.0) -> bool:
    """
    Return True if a TCP connection to host:port succeeds within timeout.

    Example:
        is_port_open("localhost", 5432)   # True if postgres is up
    """
    try:
        with socket.create_connection((host, port), timeout=timeout):
            return True
    except (socket.timeout, ConnectionRefusedError, OSError):
        return False


@dataclass
class ServiceStatus:
    host:    str
    port:    int
    latency_ms: float | None    # None if unreachable
    reachable: bool

    def __str__(self) -> str:
        if self.reachable:
            return f"{self.host}:{self.port} UP  {self.latency_ms:.1f}ms"
        return f"{self.host}:{self.port} DOWN"


def check_service(host: str, port: int, timeout: float = 2.0) -> ServiceStatus:
    """
    Check if a TCP service is reachable and measure connect latency.

    Example:
        status = check_service("db.example.com", 5432)
        print(status)
    """
    t0 = time.monotonic()
    try:
        with socket.create_connection((host, port), timeout=timeout):
            latency = (time.monotonic() - t0) * 1000
            return ServiceStatus(host=host, port=port, latency_ms=latency, reachable=True)
    except (socket.timeout, ConnectionRefusedError, OSError):
        return ServiceStatus(host=host, port=port, latency_ms=None, reachable=False)


def check_services(endpoints: list[tuple[str, int]], timeout: float = 2.0) -> list[ServiceStatus]:
    """
    Check multiple services in parallel; return list of ServiceStatus.

    Example:
        statuses = check_services([("localhost", 5432), ("localhost", 6379), ("api.svc", 8080)])
        for s in statuses:
            print(s)
    """
    from concurrent.futures import ThreadPoolExecutor, as_completed
    with ThreadPoolExecutor(max_workers=len(endpoints)) as ex:
        futures = {ex.submit(check_service, h, p, timeout): (h, p) for h, p in endpoints}
        return [fut.result() for fut in as_completed(futures)]


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    import json

    print("=== socket demo ===")

    # ── Framed echo server ────────────────────────────────────────────────────
    print("\n--- TCPServer frame echo ---")

    def echo_handler(conn: socket.socket, addr: Any) -> None:
        while True:
            try:
                data = recv_frame(conn)
                send_frame(conn, b"ECHO:" + data)
            except (ConnectionError, EOFError):
                break

    server = TCPServer("127.0.0.1", 19100, echo_handler)
    server.start()
    time.sleep(0.05)

    with tcp_client("127.0.0.1", 19100) as s:
        msg = json.dumps({"hello": "world"}).encode()
        send_frame(s, msg)
        response = recv_frame(s)
    server.stop()
    print(f"  sent: {msg[:30]}")
    print(f"  echo: {response[:35]}")

    # ── DNS resolution ────────────────────────────────────────────────────────
    print("\n--- resolve ---")
    try:
        ips = resolve("localhost")
        print(f"  localhost → {ips}")
    except Exception as e:
        print(f"  resolve: {e}")

    # ── Service check ─────────────────────────────────────────────────────────
    print("\n--- check_service ---")
    endpoints = [("127.0.0.1", 19100), ("127.0.0.1", 19101)]  # second should be closed
    for status in check_services(endpoints):
        print(f"  {status}")

    # ── is_port_open ──────────────────────────────────────────────────────────
    print("\n--- is_port_open ---")
    print(f"  localhost:65000 (unlikely open): {is_port_open('127.0.0.1', 65000, timeout=0.1)}")

    # ── UDP send ──────────────────────────────────────────────────────────────
    print("\n--- UDP (fire and forget) ---")
    try:
        udp_send("127.0.0.1", 19200, b"hello udp", timeout=0.1)
        print("  udp_send: ok (packet sent)")
    except Exception as e:
        print(f"  udp_send: {type(e).__name__} (expected if no listener)")

    print("\n=== done ===")

For the http.client alternative — http.client.HTTPConnection / HTTPSConnection implement the HTTP protocol on top of raw TCP sockets and handle chunked transfer, headers, and status parsing; use http.client or higher-level urllib.request / requests for HTTP; use socket for non-HTTP protocols (SMTP, custom binary protocols, TCP health checks) or when you need raw byte-level control. For the asyncio.streams alternative — asyncio.open_connection() and asyncio.start_server() provide the same TCP client/server pattern but with async / await coroutines instead of threads; they are more efficient for high-concurrency scenarios where many connections are idle simultaneously — use asyncio.streams for building async servers that hold many simultaneous long-lived connections; use socket with threading for simpler low-concurrency services or when you need synchronous blocking I/O. The Claude Skills 360 bundle includes socket skill sets covering tcp_connect()/tcp_client()/recv_exactly()/recv_until() connection helpers, send_frame()/recv_frame() length-prefixed framing, TCPServer threaded server class, udp_send()/udp_request() UDP helpers, and resolve()/is_port_open()/check_service()/check_services() DNS and health-check utilities. Start with the free tier to try TCP/UDP networking patterns and socket pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free