← All notes

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:btsgd6ff

iinventedlight. 2026. "urllib.request: open URLs with urlopen and Request." iinventedlight Library, iil:note:btsgd6ff, version of 2026-11-23. https://iinventedlight.com/library/notes/python-urllib-request/. License: CC-BY-4.0.

@misc{iil_note_btsgd6ff,
  author = {{iinventedlight}},
  title = {{urllib.request: open URLs with urlopen and Request}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/python-urllib-request/},
  note = {Permanent ID iil:note:btsgd6ff. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-urllib-request.md.