Claude Code for urllib.request: Python URL Fetching — Claude Skills 360 Blog
Blog / AI / Claude Code for urllib.request: Python URL Fetching
AI

Claude Code for urllib.request: Python URL Fetching

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

Python’s urllib.request module wraps http.client with a handler pipeline for redirect following, cookie management, authentication, and proxy support. from urllib.request import urlopen, Request. urlopen: resp = urllib.request.urlopen(url, timeout=10) — follows redirects, returns http.client.HTTPResponse-like object; resp.read(), resp.status, resp.getheader("Content-Type"). Request: req = urllib.request.Request(url, data=b"body", headers={"Accept": "application/json"}, method="POST"). urlretrieve: path, hdrs = urllib.request.urlretrieve(url, "local.file", reporthook=fn) — downloads to file; fn(block_num, block_size, total_size). build_opener: opener = urllib.request.build_opener(handler1, handler2) — creates a custom opener; opener.open(req). install_opener: urllib.request.install_opener(opener) — installs as global. Auth: auth = urllib.request.HTTPBasicAuthHandler(); auth.add_password(realm, uri, user, pwd). Cookies: urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()). Proxy: urllib.request.ProxyHandler({"http": "http://proxy:8080"}). Errors: urllib.error.HTTPError has .code, .reason, .headers; urllib.error.URLError has .reason. SSL: pass context=ssl.create_default_context() to urlopen. Claude Code generates authenticated API clients, file downloaders with progress, cookie-jar scrapers, and multi-handler opener chains.

CLAUDE.md for urllib.request

## urllib.request Stack
- Stdlib:  from urllib.request import urlopen, Request, build_opener
- GET:     resp = urlopen(url, timeout=10); data = resp.read()
- POST:    req = Request(url, data=json.dumps(p).encode(), headers={...}, method="POST")
- Auth:    opener = build_opener(HTTPBasicAuthHandler(mgr))
- Cookies: opener = build_opener(HTTPCookieProcessor(CookieJar()))
- Errors:  except urllib.error.HTTPError as e: e.code, e.reason

urllib.request URL Fetching Pipeline

# app/urlfetch.py — GET/POST, JSON, download, auth, cookies, retry
from __future__ import annotations

import http.cookiejar
import io
import json
import os
import ssl
import time
import urllib.error
import urllib.parse
import urllib.request
from contextlib import contextmanager
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Callable, Generator


# ─────────────────────────────────────────────────────────────────────────────
# 1. Core fetch helpers
# ─────────────────────────────────────────────────────────────────────────────

_DEFAULT_TIMEOUT = 15.0
_DEFAULT_HEADERS = {
    "User-Agent": "python-urllib.request/stdlib",
    "Accept": "*/*",
}


@dataclass
class FetchResponse:
    status:  int
    headers: dict[str, str]
    body:    bytes
    url:     str   # final URL after redirects

    @property
    def ok(self) -> bool:
        return 200 <= self.status < 300

    def text(self, encoding: str = "utf-8") -> str:
        return self.body.decode(encoding)

    def json(self) -> Any:
        return json.loads(self.body)

    def __str__(self) -> str:
        return f"HTTP {self.status} {self.url} ({len(self.body)} bytes)"


def _exec_request(
    req: urllib.request.Request,
    opener: urllib.request.OpenerDirector | None = None,
    timeout: float = _DEFAULT_TIMEOUT,
) -> FetchResponse:
    """Execute a Request and return a FetchResponse. Raises on HTTP error."""
    ctx = ssl.create_default_context()
    try:
        do_open = (opener or urllib.request).open  # type: ignore[attr-defined]
        with do_open(req, context=ctx, timeout=timeout) as resp:
            body = resp.read()
            headers = {k.lower(): v for k, v in resp.headers.items()}
            return FetchResponse(
                status=resp.status,
                headers=headers,
                body=body,
                url=resp.url,
            )
    except urllib.error.HTTPError as e:
        body = e.read() if e.fp else b""
        headers = {k.lower(): v for k, v in (e.headers or {}).items()}
        return FetchResponse(
            status=e.code,
            headers=headers,
            body=body,
            url=req.full_url,
        )


def get(
    url: str,
    *,
    params: dict[str, str] | None = None,
    headers: dict[str, str] | None = None,
    timeout: float = _DEFAULT_TIMEOUT,
    opener: urllib.request.OpenerDirector | None = None,
) -> FetchResponse:
    """
    HTTP GET request.

    Example:
        resp = get("https://httpbin.org/get", params={"q": "test"})
        print(resp.json())
    """
    if params:
        url = url + "?" + urllib.parse.urlencode(params)
    hdrs = dict(_DEFAULT_HEADERS)
    if headers:
        hdrs.update(headers)
    req = urllib.request.Request(url, headers=hdrs)
    return _exec_request(req, opener=opener, timeout=timeout)


def post(
    url: str,
    payload: Any,
    *,
    content_type: str = "application/json",
    headers: dict[str, str] | None = None,
    timeout: float = _DEFAULT_TIMEOUT,
    opener: urllib.request.OpenerDirector | None = None,
) -> FetchResponse:
    """
    HTTP POST request.

    Example:
        resp = post("https://httpbin.org/post", {"key": "val"})
        print(resp.status)
    """
    if content_type == "application/json":
        body = json.dumps(payload).encode()
    elif content_type == "application/x-www-form-urlencoded":
        body = urllib.parse.urlencode(payload).encode()
    else:
        body = payload if isinstance(payload, bytes) else str(payload).encode()
    hdrs = dict(_DEFAULT_HEADERS)
    hdrs["Content-Type"] = content_type
    hdrs["Content-Length"] = str(len(body))
    if headers:
        hdrs.update(headers)
    req = urllib.request.Request(url, data=body, headers=hdrs, method="POST")
    return _exec_request(req, opener=opener, timeout=timeout)


def fetch_json(url: str, **kwargs) -> Any:
    """
    GET URL and deserialize JSON body.

    Example:
        data = fetch_json("https://httpbin.org/json")
    """
    resp = get(url, **kwargs)
    if not resp.ok:
        raise RuntimeError(f"HTTP {resp.status}: {resp.url}")
    return resp.json()


# ─────────────────────────────────────────────────────────────────────────────
# 2. Download helpers
# ─────────────────────────────────────────────────────────────────────────────

@dataclass
class DownloadProgress:
    filename: str
    downloaded: int = 0
    total: int = -1   # -1 if Content-Length unknown

    @property
    def percent(self) -> float | None:
        if self.total > 0:
            return self.downloaded / self.total * 100
        return None

    def __str__(self) -> str:
        if self.total > 0:
            return f"{self.filename}: {self.downloaded}/{self.total} ({self.percent:.1f}%)"
        return f"{self.filename}: {self.downloaded} bytes"


def download_file(
    url: str,
    dest: str | Path,
    *,
    chunk_size: int = 65536,
    timeout: float = 30.0,
    on_progress: Callable[[DownloadProgress], None] | None = None,
) -> Path:
    """
    Download URL to dest file with optional progress callback.

    Example:
        download_file("https://example.com/file.gz", "/tmp/file.gz",
                      on_progress=lambda p: print(p))
    """
    dest_path = Path(dest)
    ctx = ssl.create_default_context()
    req = urllib.request.Request(url, headers=dict(_DEFAULT_HEADERS))
    with urllib.request.urlopen(req, context=ctx, timeout=timeout) as resp:
        total = int(resp.getheader("Content-Length") or "-1")
        progress = DownloadProgress(filename=dest_path.name, total=total)
        dest_path.parent.mkdir(parents=True, exist_ok=True)
        with dest_path.open("wb") as fout:
            while True:
                chunk = resp.read(chunk_size)
                if not chunk:
                    break
                fout.write(chunk)
                progress.downloaded += len(chunk)
                if on_progress:
                    on_progress(progress)
    return dest_path


# ─────────────────────────────────────────────────────────────────────────────
# 3. Opener factories
# ─────────────────────────────────────────────────────────────────────────────

def basic_auth_opener(
    url: str,
    username: str,
    password: str,
    realm: str = "",
) -> urllib.request.OpenerDirector:
    """
    Build an opener with HTTP Basic auth for url.

    Example:
        opener = basic_auth_opener("https://api.example.com", "user", "secret")
        resp = get("https://api.example.com/data", opener=opener)
    """
    mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    mgr.add_password(realm or None, url, username, password)
    auth_handler = urllib.request.HTTPBasicAuthHandler(mgr)
    return urllib.request.build_opener(auth_handler)


def cookie_opener(
    jar: http.cookiejar.CookieJar | None = None,
) -> tuple[urllib.request.OpenerDirector, http.cookiejar.CookieJar]:
    """
    Build an opener with a CookieJar for session persistence.
    Returns (opener, jar) so the jar can be inspected or saved.

    Example:
        opener, jar = cookie_opener()
        get("https://example.com/login", opener=opener)
        print(list(jar))   # inspect stored cookies
    """
    cjar = jar or http.cookiejar.CookieJar()
    handler = urllib.request.HTTPCookieProcessor(cjar)
    return urllib.request.build_opener(handler), cjar


def proxy_opener(proxies: dict[str, str]) -> urllib.request.OpenerDirector:
    """
    Build an opener that routes traffic through proxies.

    Example:
        opener = proxy_opener({"http": "http://proxy.corp:8080",
                               "https": "http://proxy.corp:8080"})
        resp = get("http://internal.corp/api", opener=opener)
    """
    return urllib.request.build_opener(urllib.request.ProxyHandler(proxies))


# ─────────────────────────────────────────────────────────────────────────────
# 4. Retry and error handling
# ─────────────────────────────────────────────────────────────────────────────

def get_with_retry(
    url: str,
    max_attempts: int = 3,
    backoff: float = 1.0,
    retry_statuses: tuple[int, ...] = (429, 500, 502, 503, 504),
    **kwargs,
) -> FetchResponse:
    """
    GET with exponential-backoff retry on network errors and transient HTTP statuses.

    Example:
        resp = get_with_retry("https://api.example.com/data", max_attempts=4)
    """
    delay = backoff
    last: FetchResponse | None = None
    for attempt in range(max_attempts):
        try:
            resp = get(url, **kwargs)
            if resp.status not in retry_statuses:
                return resp
            last = resp
        except (urllib.error.URLError, OSError, TimeoutError):
            if attempt == max_attempts - 1:
                raise
        if attempt < max_attempts - 1:
            time.sleep(delay)
            delay *= 2.0
    if last is not None:
        return last
    raise RuntimeError(f"All {max_attempts} attempts failed for {url}")


# ─────────────────────────────────────────────────────────────────────────────
# 5. Utility helpers
# ─────────────────────────────────────────────────────────────────────────────

def check_url(url: str, timeout: float = 5.0) -> tuple[bool, int]:
    """
    Return (reachable, status_code). HEAD request; falls back to GET.

    Example:
        ok, code = check_url("https://example.com")
        print(ok, code)
    """
    try:
        ctx = ssl.create_default_context()
        req = urllib.request.Request(url, method="HEAD", headers=dict(_DEFAULT_HEADERS))
        with urllib.request.urlopen(req, context=ctx, timeout=timeout) as resp:
            return True, resp.status
    except urllib.error.HTTPError as e:
        return e.code < 400, e.code
    except Exception:
        return False, 0


def read_url_lines(url: str, encoding: str = "utf-8", timeout: float = 10.0) -> list[str]:
    """
    Fetch a text URL and return lines (strips blank lines).

    Example:
        lines = read_url_lines("https://example.com/list.txt")
    """
    resp = get(url, timeout=timeout)
    if not resp.ok:
        raise RuntimeError(f"HTTP {resp.status}: {url}")
    return [ln for ln in resp.text(encoding).splitlines() if ln.strip()]


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

if __name__ == "__main__":
    print("=== urllib.request demo ===")

    # ── GET + JSON ────────────────────────────────────────────────────────────
    print("\n--- fetch_json ---")
    try:
        data = fetch_json("https://httpbin.org/json")
        print(f"  keys: {list(data.keys())[:4]}")
    except Exception as e:
        print(f"  network error: {e} (offline?)")

    # ── POST ──────────────────────────────────────────────────────────────────
    print("\n--- POST ---")
    try:
        resp = post("https://httpbin.org/post", {"name": "urllib.request", "version": 3})
        print(f"  status={resp.status}  ok={resp.ok}")
        echo = resp.json().get("json", {})
        print(f"  echo: {echo}")
    except Exception as e:
        print(f"  network error: {e} (offline?)")

    # ── check_url ─────────────────────────────────────────────────────────────
    print("\n--- check_url ---")
    for url in ["https://httpbin.org/status/200", "https://httpbin.org/status/404"]:
        try:
            ok, code = check_url(url, timeout=5.0)
            print(f"  {url.split('/')[-1]:>4} → ok={ok} code={code}")
        except Exception as e:
            print(f"  {url} error: {e}")

    # ── cookie_opener (smoke test) ────────────────────────────────────────────
    print("\n--- cookie_opener ---")
    try:
        opener, jar = cookie_opener()
        resp = get("https://httpbin.org/cookies/set?token=abc123", opener=opener)
        print(f"  status={resp.status}  cookies={len(list(jar))}")
    except Exception as e:
        print(f"  network error: {e} (offline?)")

    # ── download to bytes (simulate with StringIO target) ─────────────────────
    print("\n--- download_file ---")
    try:
        import tempfile
        with tempfile.NamedTemporaryFile(suffix=".json", delete=True) as tf:
            path = download_file(
                "https://httpbin.org/json",
                tf.name,
                on_progress=lambda p: None,
            )
        print(f"  downloaded {path.name}: ok (file cleaned up)")
    except Exception as e:
        print(f"  network error: {e} (offline?)")

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

For the requests / httpx alternative — requests (PyPI) collapses the entire handler chain into a one-line API (requests.get(url, auth=..., proxies=..., cookies=...)), handles streaming with stream=True and iter_content(), and adds automatic status-code assertion with raise_for_status(); httpx extends this with async support and HTTP/2 — use requests or httpx in any application where adding PyPI dependencies is acceptable; use urllib.request when you need zero external dependencies for a library or container image, or for simple script-level downloading without the overhead of a full HTTP client. For the http.client alternative — http.client is the lower layer that urllib.request builds on top of; it exposes persistent connections, explicit conn.request() / conn.getresponse() calls, and responses before body consumption, which allows HEAD → decision → read patterns that urlopen cannot express — use http.client when you need connection reuse across multiple requests to the same host, byte-level wire control, or batch requests that share a keep-alive session; use urllib.request for single-shot fetches with redirect following, cookie handling, or Basic auth without managing connection lifecycle manually. The Claude Skills 360 bundle includes urllib.request skill sets covering FetchResponse dataclass, get()/post()/fetch_json() core methods, DownloadProgress with download_file() and byte-range streaming, basic_auth_opener()/cookie_opener()/proxy_opener() handler factories, get_with_retry() with exponential backoff, and check_url()/read_url_lines() utilities. Start with the free tier to try URL fetching patterns and urllib.request 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