The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world -- basic and digest authentication, redirections, cookies and more.
The urllib2 module defines the following functions:
url[, data]) |
data may be a string specifying additional data to send to the server. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.
This function returns a file-like object with two additional methods:
Raises URLError on errors.
Note that None
may be returned if no handler handles the
request (though the default installed global OpenerDirector
uses UnknownHandler to ensure this never happens).
opener) |
[handler, ...]) |
If the Python installation has SSL support (socket.ssl() exists), HTTPSHandler will also be added.
Beginning in Python 2.3, a BaseHandler subclass may also
change its handler_order member variable to modify its
position in the handlers list. Besides ProxyHandler, which has
handler_order of 100
, all handlers currently have it
set to 500
.
The following exceptions are raised as appropriate:
The following classes are provided:
url[, data][, headers] [, origin_req_host][, unverifiable]) |
url should be a string which is a valid URL. For a description of data see the add_data() description. headers should be a dictionary, and will be treated as if add_header() was called with each key and value as arguments.
The final two arguments are only of interest for correct handling of third-party HTTP cookies:
origin_req_host should be the request-host of the origin
transaction, as defined by RFC 2965. It defaults to
cookielib.request_host(self)
. This is the host name or IP
address of the original request that was initiated by the user. For
example, if the request is for an image in an HTML document, this
should be the request-host of the request for the page containing the
image.
unverifiable should indicate whether the request is unverifiable, as defined by RFC 2965. It defaults to False. An unverifiable request is one whose URL the user did not have the option to approve. For example, if the request is for an image in an HTML document, and the user had no option to approve the automatic fetching of the image, this should be true.
) |
) |
) |
) |
[cookiejar]) |
[proxies]) |
) |
(realm, uri) -> (user, password)
mappings.
) |
(realm, uri) -> (user, password)
mappings.
A realm of None
is considered a catch-all realm, which is searched
if no other realm fits.
[password_mgr]) |
[password_mgr]) |
[password_mgr]) |
[password_mgr]) |
[password_mgr]) |
[password_mgr]) |
) |
) |
) |
) |
) |
) |
) |