---
source: https://iinventedlight.com/library/notes/python-urllib-request.md
title: "urllib.request: open URLs with urlopen and Request"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, urllib.request — Extensible library for opening URLs; CPython, urllib.request documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-urllib-request"
tags: [python, urllib, urlopen, http, request]
client: Librarian
sources:
  - id: S1
    title: "urllib.request — Extensible library for opening URLs"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/urllib.request.html
    accessed: 2026-11-23
    license: "PSF License Version 2"
  - id: S2
    title: "urllib.request documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/urllib.request.rst
    accessed: 2026-11-23
    license: "PSF License Version 2"
---
# urllib.request: open URLs with urlopen and Request
**Answer:** urlopen opens a URL given as a string or a Request object. An OpenerDirector opens URLs through chained handlers. Install a custom opener only if you want urlopen to use it.

## urlopen and openers

Pass urlopen either a Request object or a string that holds a valid, properly encoded URL.

Python 2.6 and earlier had a legacy urllib.urlopen, now discontinued. The replacement, urllib.request.urlopen, matches the old urllib2.urlopen.

install_opener installs an OpenerDirector instance as the default global opener.

BaseHandlers chained together are how OpenerDirector opens URLs.

Call OpenerDirector.open() instead of urlopen() unless you want urlopen to use that custom opener.

OpenerDirector.open shares urlopen's arguments, return values, and exceptions, because urlopen just calls open on the installed global opener.

add_header is often used to spoof the User Agent header value. Some HTTP servers only allow requests coming from common browsers as opposed to scripts.

## Headers, data, and proxies

HTTP/1.1 is what urllib.request uses, and its requests include a Connection:close header.

When proxy settings are detected, such as an http_proxy environment variable, a default ProxyHandler is installed so requests go through the proxy.

Currently HTTP requests are the only ones that use data.

For an HTTP POST, data should be a buffer in the standard application/x-www-form-urlencoded format.

## See also

- [python-http-client](https://iinventedlight.com/library/notes/python-http-client/)
- [python-urllib-parse](https://iinventedlight.com/library/notes/python-urllib-parse/)
- [python-ssl](https://iinventedlight.com/library/notes/python-ssl/)

## Claims

- id: C1
  claim: urlopen can take a URL string or a Request object.
  source: S1
  evidence: "or a Request object"

- id: C2
  claim: install_opener sets an OpenerDirector as the default global opener.
  source: S1
  evidence: "as the default global opener"

- id: C3
  claim: OpenerDirector opens URLs via handlers chained together.
  source: S1
  evidence: "chained together"

- id: C4
  claim: The module uses HTTP/1.1 and includes a Connection:close header.
  source: S2
  evidence: "uses HTTP/1.1 and includes"

- id: C5
  claim: HTTP requests are the only ones that use data.
  source: S1
  evidence: "HTTP requests are the only ones that use data"

## Sources

- [S1] urllib.request — Extensible library for opening URLs. Python Software Foundation. https://docs.python.org/3/library/urllib.request.html . License: PSF License Version 2. Accessed 2026-11-23.

- [S2] urllib.request documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/urllib.request.rst . License: PSF License Version 2. Accessed 2026-11-23.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, urllib.request — Extensible library for opening URLs; CPython, urllib.request documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
