all Python 3urllib.requestfunction
urllib.request.build_opener()
openers
Builds an OpenerDirector from the standard handlers plus yours — the way to set up proxies, cookies, authentication and headers.
Syntax
opener = urllib.request.build_opener(handler, ...)
A handler you pass replaces the standard one of the same class or a subclass instead of sitting next to it; you may pass a class, and it is created without arguments. ProxyHandler joins only when *_proxy variables are set, so the same program talks to the network differently on different machines; ProxyHandler({}) turns proxies off explicitly. The result affects only opener.open() — urlopen() does not know about it without install_opener().
Examples
import urllib.request
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
names = [type(h).__name__ for h in opener.handlers]
print('ProxyHandler' in names, 'DataHandler' in names) # → False True
print(opener.open('data:,hi').read()) # → b'hi'
print(opener.addheaders) # → ? [('User-agent', 'Python-urllib/3.14')]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy