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 and 307 redirect errors, and the HTTPDigestAuthHandler deals with digest authentication.
objects of interest: OpenerDirector --
BaseHandler --
exceptions: URLError-- a subclass of IOError, individual protocols have their own specific subclass
HTTPError-- also a valid HTTP response, so you can treat an HTTP error as an exceptional event or valid response
internals: BaseHandler and parent _call_chain conventions
Example usage:
import urllib2
# set up authentication info
authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password(realm
, host
, username
, password
)
proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"})
# build a new opener that adds authentication and caching FTP handlers opener = urllib2.build_opener(proxy_support, authinfo, urllib2.CacheFTPHandler)
# install it urllib2.install_opener(opener)
f = urllib2.urlopen(http://www.python.org/
)