PyDocs

Internet Protocols and Support

urllib.request

not WASI

An extensible library for opening URLs using a variety of protocols

The simplest way to use this module is to call the urlopen function,
which accepts a string containing a URL or a Request object (described
below).  It opens the URL and returns the results as file-like
object; the returned object has some extra methods described below.

The OpenerDirector manages a collection of Handler objects that do
all the actual work.  Each Handler implements a particular protocol or
option.  The OpenerDirector is a composite object that invokes the
Handlers needed to open the requested URL.  For example, the
HTTPHandler performs HTTP GET and POST requests and deals with
non-error returns.  The HTTPRedirectHandler automatically deals with
HTTP 301, 302, 303, 307, and 308 redirect errors, and the
HTTPDigestAuthHandler deals with digest authentication.

urlopen(url, data=None) -- Basic usage is the same as original
urllib.  pass the url and optionally data to post to an HTTP URL, and
get a file-like object back.  One difference is that you can also pass
a Request instance instead of URL.  Raises a URLError (subclass of
OSError); for HTTP errors, raises an HTTPError, which can also be
treated as a valid response.

build_opener -- Function that creates a new OpenerDirector instance.
Will install the default handlers.  Accepts one or more Handlers as
arguments, either instances or Handler classes that it will
instantiate.  If one of the argument is a subclass of the default
handler, the argument will be installed instead of the default.

install_opener -- Installs a new opener as the default opener.

objects of interest:

OpenerDirector -- Sets up the User Agent as the Python-urllib client and manages
the Handler classes, while dealing with requests and responses.

Request -- An object that encapsulates the state of a request.  The
state can be as simple as the URL.  It can also include extra HTTP
headers, e.g. a User-Agent.

BaseHandler --

internals:
BaseHandler and parent
_call_chain conventions

Example usage:

import urllib.request

# set up authentication info
authinfo = urllib.request.HTTPBasicAuthHandler()
authinfo.add_password(realm='PDQ Application',
                      uri='https://mahler:8092/site-updates.py',
                      user='klem',
                      passwd='geheim$parole')

proxy_support = urllib.request.ProxyHandler({"http" : "http://ahad-haam:3128"})

# build a new opener that adds authentication and caching FTP handlers
opener = urllib.request.build_opener(proxy_support, authinfo,
                                     urllib.request.CacheFTPHandler)

# install it
urllib.request.install_opener(opener)

f = urllib.request.urlopen('https://www.python.org/')
Symbols in urllib.request

a

AbstractBasicAuthHandler
This is a mixin class that helps with HTTP authentication, both to the remote host and to a proxy.
AbstractDigestAuthHandler
This is a mixin class that helps with HTTP authentication, both to the remote host and to a proxy.

b

BaseHandler
This is the base class for all registered handlers — and handles only the simple mechanics of registration.
build_opener
Create an opener object from a list of handlers.

c

CacheFTPHandler
Open FTP URLs, keeping a cache of open FTP connections to minimize delays.

d

DataHandler
Open data URLs.

f

FileHandler
Open local files.
FTPHandler
Open FTP URLs.

g

getproxies
Return a dictionary of scheme -> proxy server URL mappings.

h

HTTPBasicAuthHandler
Handle authentication with the remote host.
HTTPCookieProcessor
A class to handle HTTP Cookies.
HTTPDefaultErrorHandler
A class which defines a default handler for HTTP error responses; all responses are turned into HTTPError exceptions.
HTTPDigestAuthHandler
An authentication protocol defined by RFC 2069
HTTPErrorProcessor
Process HTTP error responses.
HTTPHandler
A class to handle opening of HTTP URLs.
HTTPPasswordMgr
Keep a database of (realm, uri) -> (user, password) mappings.
HTTPPasswordMgrWithDefaultRealm
Keep a database of (realm, uri) -> (user, password) mappings.
HTTPPasswordMgrWithPriorAuth
A variant of HTTPPasswordMgrWithDefaultRealm that also has a database of uri -> is_authenticated mappings.
HTTPRedirectHandler
A class to handle redirections.
HTTPSHandler
A class to handle opening of HTTPS URLs.

i

install_opener
Install an OpenerDirector instance as the default global opener.

o

OpenerDirector
The OpenerDirector class opens URLs via BaseHandlers chained together.

p

pathname2url
Convert the given local file system path to a file URL.
ProxyBasicAuthHandler
Handle authentication with the proxy.
ProxyDigestAuthHandler
Handle authentication with the proxy.
ProxyHandler
Cause requests to go through a proxy.

r

Request
This class is an abstraction of a URL request.

u

UnknownHandler
A catch-all class to handle unknown URLs.
url2pathname
Convert the given file URL to a local file system path.
urlcleanup
Clean up temporary files from urlretrieve calls.
urlopen
Open the URL url, which can be either a string or a Request object.
urlretrieve
Retrieve a URL into a temporary location on disk.