(continued from previous page) 

password_mgr .add_password( None , top_level_url, username, password) handler =urllib.request.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) opener =urllib.request.build_opener(handler) # use the opener to fetch a URL opener.open(a_url) # Install the opener. # Now all calls to urllib.request.urlopen use our opener. urllib.request.install_opener(opener) 

Note: In the above example we only supplied our$_{HTTPBasicAuthHandler}$ to$_{build_opener}$ . By default openers have the handlers for normal situations – ProxyHandler (if a proxy setting such as an http_proxy environment variable is set), UnknownHandler , HTTPHandler , HTTPDefaultErrorHandler , HTTPRedirectHandler ,$_{FTPHandler}$ ,$_{FileHandler}$,$_{DataHandler}$ ,$_{HTTPErrorProcessor}$ . 

top_level_url is in fact either a full URL (including the ‘http:’ scheme component and the hostname and optionally the port number) e.g. "http://example.com/" or an “authority” (i.e. the hostname, option- ally including the port number) e.g. "example.com" or "example.com:8080" (the latter example in- cludes a port number). The authority, if present, must NOT contain the “userinfo” component - for example "joe:password@example.com" is not correct. 

## 7 Proxies 

urllib will auto-detect your proxy settings and use those. This is through the$_{ProxyHandler}$, which is part of the normal handler chain when a proxy setting is detected. Normally that’s a good thing, but there are occasions when it may not be helpful$^{5}$. One way to do this is to setup our own$_{ProxyHandler}$, with no proxies defined. This is done using similar steps to setting up a Basic Authentication handler: 

>>> proxy_support=urllib.request.ProxyHandler({}) >>> opener =urllib.request.build_opener(proxy_support) >>> urllib.request.install_opener(opener) 

Note: Currently$_{urllib.request}$ does not support fetching of$_{https}$ locations through a proxy. However, this can be enabled by extending urllib.request as shown in the recipe$^{6}$. 

Note: HTTP_PROXY will be ignored if a variable REQUEST_METHOD is set; see the documentation on getproxies() . 

