How to handle cookies, when fetching web pages with Python.
**urllib2** is a Python_ module for fetching URLs (Uniform Resource
Locators). It offers a very simple interface, in the form of the
*urlopen* function. This is capable of fetching URLs using a variety
of different protocols. It also offers a slightly more complex
interface for handling common situations - like basic authentication,
cookies, proxies, and so on. These are provided by objects called
handlers and openers.
This HOWTO is written by `Michael Foord <http://www.voidspace.org.uk/python/index.shtml>`_.
While urllib2 supports fetching URLs for many "URL schemes"
(identified by the string before the ":" in URL - e.g. "ftp" is the
URL scheme of "ftp://python.org/") using their associated network
protocols (e.g. FTP, HTTP), this tutorial focuses on the most common
case, HTTP.
**urllib2** is a Python_ module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the *urlopen* function. This is capable of fetching URLs using a variety of different protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies, and so on. These are provided by objects called handlers and openers.
For straightforward situations *urlopen* is very easy to use. But as
soon as you encounter errors or non-trivial cases when opening HTTP
For straightforward situations *urlopen* is very easy to use. But as soon as you encounter errors, or non-trivial cases, you will need some understanding of the HyperText Transfer Protocol. The most comprehensive reference to HTTP is :RFC:`2616`. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using *urllib2*, with enough detail about HTTP to help you through. It is not intended to replace the `urllib2 docs`_ [#]_, but is supplementary to them.
URLs, you will need some understanding of the HyperText Transfer
Protocol. The most comprehensive and authoritative reference to HTTP
is :RFC:`2616`. This is a technical document and not intended to be
easy to read. This HOWTO aims to illustrate using *urllib2*, with
enough detail about HTTP to help you through. It is not intended to
replace the `urllib2 docs`_ , but is supplementary to them.
Fetching URLs
Fetching URLs
=============
=============
HTTP is based on requests and responses - the client makes requests and servers send responses. Python mirrors this by having you form a ``Request`` object which represents the request you are making. In it's simplest form you create a Request object that specifies the URL you want to fetch [#]_. Calling ``urlopen`` with this Request object returns a handle on the page requested. This handle is a file like object : ::
The simplest way to use urllib2 is as follows : ::
import urllib2
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
Many uses of urllib2 will be that simple (note that instead of an
'http:' URL we could have used an URL starting with 'ftp:', 'file:',
etc.). However, it's the purpose of this tutorial to explain the more
complicated cases, concentrating on HTTP.
HTTP is based on requests and responses - the client makes requests
and servers send responses. urllib2 mirrors this with a ``Request``
object which represents the HTTP request you are making. In its
simplest form you create a Request object that specifies the URL you
want to fetch. Calling ``urlopen`` with this Request object returns a
response object for the URL requested. This response is a file-like
object, which means you can for example call .read() on the response :
::
the_url = 'http://www.voidspace.org.uk'
import urllib2
req = urllib2.Request(the_url)
handle = urllib2.urlopen(req)
the_page = handle.read()
There are two extra things that Request objects allow you to do. Sometimes you want to **POST** data to a CGI (Common Gateway Interface) [#]_ or other web application. This is what your browser does when you fill in a FORM on the web. You may be mimicking a FORM submission, or transmitting data to your own application. In either case the data needs to be encoded for safe transmission over HTTP, and then passed to the Request object as the ``data`` argument. The encoding is done using a function from the ``urllib`` library *not* from ``urllib2``. ::
Note that other encodings are sometimes required (e.g. for file upload
from HTML forms - see `HTML Specification, Form Submission`_ for more
details).
If you do not pass the ``data`` argument, urllib2 uses a **GET**
request. One way in which GET and POST requests differ is that POST
requests often have "side-effects": they change the state of the
system in some way (for example by placing an order with the website
for a hundredweight of tinned spam to be delivered to your door).
Though the HTTP standard makes it clear that POSTs are intended to
*always* cause side-effects, and GET requests *never* to cause
side-effects, nothing prevents a GET request from having side-effects,
nor a POST requests from having no side-effects. Data can also be
passed in an HTTP request by encoding it in the URL itself.
Headers
-------
We'll discuss here one particular HTTP header, to illustrate how to
add headers to your HTTP request.
Some websites [#]_ dislike being browsed by programs, or send different versions to different browsers [#]_ . By default urllib2 identifies itself as ``Python-urllib/2.4``, which may confuse the site, or just plain not work. The way a browser identifies itself is through the ``User-Agent`` header [#]_. When you create a Request object you can pass a dictionary of headers in. The following example makes the same request as above, but identifies itself as a version of Internet Explorer [#]_. ::
Some websites [#]_ dislike being browsed by programs, or send
different versions to different browsers [#]_ . By default urllib2
identifies itself as ``Python-urllib/x.y`` (where ``x`` and ``y`` are
the major and minor version numbers of the Python release,
e.g. ``Python-urllib/2.5``), which may confuse the site, or just plain
not work. The way a browser identifies itself is through the
``User-Agent`` header [#]_. When you create a Request object you can
pass a dictionary of headers in. The following example makes the same
request as above, but identifies itself as a version of Internet
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'Michael Foord',
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'location' : 'Northampton',
...
@@ -75,38 +164,38 @@ Some websites [#]_ dislike being browsed by programs, or send different versions
...
@@ -75,38 +164,38 @@ Some websites [#]_ dislike being browsed by programs, or send different versions
headers = { 'User-Agent' : user_agent }
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
data = urllib.urlencode(values)
req = urllib2.Request(the_url, data, headers)
req = urllib2.Request(url, data, headers)
handle = urllib2.urlopen(req)
response = urllib2.urlopen(req)
the_page = handle.read()
the_page = response.read()
The handle also has two useful methods. See the section on `info and geturl`_ which comes after we have a look at what happens when things go wrong.
The response also has two useful methods. See the section on `info and
geturl`_ which comes after we have a look at what happens when things
go wrong.
Coping With Errors
Handling Exceptions
==================
===================
*urlopen* raises ``URLError`` or ``HTTPError`` in the event of an error. ``HTTPError`` is a subclass of ``URLError``, which is a subclass of ``IOError``. This means you can trap for ``IOError`` if you want. ::
*urlopen* raises ``URLError`` when it cannot handle a response (though
as usual with Python APIs, builtin exceptions such as ValueError,
TypeError etc. may also be raised).
req = urllib2.Request(some_url)
``HTTPError`` is the subclass of ``URLError`` raised in the specific
try:
case of HTTP URLs.
handle = urllib2.urlopen(req)
except IOError:
print 'Something went wrong'
else:
print handle.read()
URLError
URLError
--------
--------
If the request fails to reach a server then urlopen will raise a ``URLError``. This will usually be because there is no network connection (no route to the specified server), or the specified server doesn't exist.
Often, URLError is raised because there is no network connection (no
route to the specified server), or the specified server doesn't exist.
In this case, the exception raised will have a 'reason' attribute, which is a tuple containing an error code and a text error message.
In this case, the exception raised will have a 'reason' attribute,
which is a tuple containing an error code and a text error message.
If the request reaches a server, but the server is unable to fulfil the request, it returns an error code. The default handlers will hande some of these errors for you. For those it can't handle, urlopen will raise an ``HTTPError``. Typical errors include '404' (page not found), '403' (request forbidden), and '401' (authentication required).
Every HTTP response from the server contains a numeric "status
code". Sometimes the status code indicates that the server is unable
to fulfil the request. The default handlers will handle some of these
responses for you (for example, if the response is a "redirection"
that requests the client fetch the document from a different URL,
urllib2 will handle that for you). For those it can't handle, urlopen
will raise an ``HTTPError``. Typical errors include '404' (page not
found), '403' (request forbidden), and '401' (authentication
required).
See http://www.w3.org/Protocols/HTTP/HTRESP.html for a reference on all the http error codes.
See section 10 of RFC 2616 for a reference on all the HTTP error
codes.
The ``HTTPError`` instance raised will have an integer 'code' attribute, which corresponds to the error sent by the server.
The ``HTTPError`` instance raised will have an integer 'code'
attribute, which corresponds to the error sent by the server.
There is a useful dictionary of response codes in ``HTTPBaseServer``, that shows all the defined response codes. Because the default handlers handle redirects (codes in the 300 range), and codes in the 100-299 range indicate success, you will usually only see error codes in the 400-599 range.
Error Codes
Error Codes
~~~~~~~~~~~
~~~~~~~~~~~
.. note::
Because the default handlers handle redirects (codes in the 300
range), and codes in the 100-299 range indicate success, you will
As of Python 2.5 a dictionary like this one has become part of ``urllib2``.
usually only see error codes in the 400-599 range.
::
``BaseHTTPServer.BaseHTTPRequestHandler.responses`` is a useful
dictionary of response codes in that shows all the response codes used
by RFC 2616. The dictionary is reproduced here for convenience ::
# Table mapping response codes to messages; entries have the
# Table mapping response codes to messages; entries have the
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not modified',
304: ('Not Modified',
'Document has not changed since given time'),
'Document has not changed since given time'),
305: ('Use Proxy',
305: ('Use Proxy',
'You must use proxy specified in Location'
'You must use proxy specified in Location to access this '
' to access this resource.'),
'resource.'),
307: ('Temporary Redirect',
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'),
'Object moved temporarily -- see URI list'),
400: ('Bad request',
400: ('Bad Request',
'Bad request syntax or unsupported method'),
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
401: ('Unauthorized',
'No permission -- see authorization schemes'),
'No permission -- see authorization schemes'),
402: ('Payment required',
402: ('Payment Required',
'No payment -- see charging schemes'),
'No payment -- see charging schemes'),
403: ('Forbidden',
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
405: ('Method Not Allowed',
'Specified method is invalid for this server.'),
'Specified method is invalid for this server.'),
406: ('Not Acceptable',
406: ('Not Acceptable', 'URI not available in preferred format.'),
'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
407: ('Proxy Authentication Required',
'You must authenticate with '
'this proxy before proceeding.'),
'this proxy before proceeding.'),
408: ('Request Time-out',
408: ('Request Timeout', 'Request timed out; try again later.'),
'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed',
412: ('Precondition Failed', 'Precondition in headers is false.'),
'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type',
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
'Cannot satisfy request range.'),
417: ('Expectation Failed',
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),
'Expect condition could not be satisfied.'),
500: ('Internal error', 'Server got itself in trouble'),
500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
501: ('Not Implemented',
'Server does not support this operation'),
'Server does not support this operation'),
502: ('Bad Gateway',
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
503: ('Service temporarily overloaded',
'The server cannot process the request due to a high load'),
'The server cannot '
504: ('Gateway Timeout',
'process the request due to a high load'),
504: ('Gateway timeout',
'The gateway server did not receive a timely response'),
'The gateway server did not receive a timely response'),
505: ('HTTP Version not supported', 'Cannot fulfill request.'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}
}
When an error is raised the server responds by returning an http error code *and* an error page. You can use the ``HTTPError`` instance as a handle on the page returned. This means that as well as the code attribute, it also has read, geturl, and info, methods. ::
When an error is raised the server responds by returning an HTTP error
code *and* an error page. You can use the ``HTTPError`` instance as a
response on the page returned. This means that as well as the code
attribute, it also has read, geturl, and info, methods. ::
@@ -229,8 +322,8 @@ When an error is raised the server responds by returning an http error code *and
...
@@ -229,8 +322,8 @@ When an error is raised the server responds by returning an http error code *and
Wrapping it Up
Wrapping it Up
--------------
--------------
So if you want to be prepared for ``HTTPError`` *or* ``URLError`` there are two
So if you want to be prepared for ``HTTPError`` *or* ``URLError``
basic approaches. I prefer the second approach.
there are two basic approaches. I prefer the second approach.
Number 1
Number 1
~~~~~~~~
~~~~~~~~
...
@@ -241,7 +334,7 @@ Number 1
...
@@ -241,7 +334,7 @@ Number 1
from urllib2 import Request, urlopen, URLError, HTTPError
from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
req = Request(someurl)
try:
try:
handle = urlopen(req)
response = urlopen(req)
except HTTPError, e:
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
print 'Error code: ', e.code
...
@@ -254,7 +347,8 @@ Number 1
...
@@ -254,7 +347,8 @@ Number 1
.. note::
.. note::
The ``except HTTPError`` *must* come first, otherwise ``except URLError`` will *also* catch an ``HTTPError``.
The ``except HTTPError`` *must* come first, otherwise ``except URLError``
will *also* catch an ``HTTPError``.
Number 2
Number 2
~~~~~~~~
~~~~~~~~
...
@@ -264,8 +358,8 @@ Number 2
...
@@ -264,8 +358,8 @@ Number 2
from urllib2 import Request, urlopen
from urllib2 import Request, urlopen
req = Request(someurl)
req = Request(someurl)
try:
try:
handle = urlopen(req)
response = urlopen(req)
except IOError, e:
except URLError, e:
if hasattr(e, 'reason'):
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'We failed to reach a server.'
print 'Reason: ', e.reason
print 'Reason: ', e.reason
...
@@ -279,110 +373,180 @@ Number 2
...
@@ -279,110 +373,180 @@ Number 2
info and geturl
info and geturl
===============
===============
The handle returned by urlopen (or the ``HTTPError`` instance) has two useful methods ``info`` and ``geturl``.
The response returned by urlopen (or the ``HTTPError`` instance) has
two useful methods ``info`` and ``geturl``.
**geturl** - this returns the real url of the page fetched. This is useful because ``urlopen`` (or the openener object used) may have followed a redirect. The url of the page fetched may not be the same as the url requested.
**geturl** - this returns the real URL of the page fetched. This is
useful because ``urlopen`` (or the opener object used) may have
followed a redirect. The URL of the page fetched may not be the same
as the URL requested.
**info** - this returns a dictionary like object that describes the page fetched, particularly the headers sent by the server. It is actually an ``httplib.HTTPMessage`` instance. In versions of Python prior to 2.3.4 it wasn't safe to iterate over the object directly, so you should iterate over the list returned by ``msg.keys()`` instead.
**info** - this returns a dictionary-like object that describes the
page fetched, particularly the headers sent by the server. It is
currently an ``httplib.HTTPMessage`` instance.
Typical headers include 'content-length', 'content-type', and so on. See the `Quick Reference to HTTP Headers`_ for a useful reference on the different sort of headers.
Typical headers include 'Content-length', 'Content-type', and so
on. See the `Quick Reference to HTTP Headers`_ for a useful listing of
HTTP headers with brief explanations of their meaning and use.
Openers and Handlers
Openers and Handlers
====================
====================
Openers and handlers are slightly esoteric parts of **urllib2**. When you fetch a URL you use an opener. Normally we have been using the default opener - via ``urlopen`` - but you can create custom openers. Openers use handlers.
When you fetch a URL you use an opener (an instance of the perhaps
confusingly-named urllib2.OpenerDirector). Normally we have been using
the default opener - via ``urlopen`` - but you can create custom
openers. Openers use handlers. All the "heavy lifting" is done by the
handlers. Each handler knows how to open URLs for a particular URL
scheme (http, ftp, etc.), or how to handle an aspect of URL opening,
for example HTTP redirections or HTTP cookies.
You will want to create openers if you want to fetch URLs with
specific handlers installed, for example to get an opener that handles
cookies, or to get an opener that does not handle redirections.
``build_opener`` is used to create ``opener`` objects - for fetching URLs with specific handlers installed. Handlers can handle cookies, authentication, and other common but slightly specialised situations. Opener objects have an ``open`` method, which can be called directly to fetch urls in the same way as the ``urlopen`` function.
To create an opener, instantiate an OpenerDirector, and then call
.add_handler(some_handler_instance) repeatedly.
``install_opener`` can be used to make an ``opener`` object the default opener. This means that calls to ``urlopen`` will use the opener you have installed.
Alternatively, you can use ``build_opener``, which is a convenience
function for creating opener objects with a single function call.
``build_opener`` adds several handlers by default, but provides a
quick way to add more and/or override the default handlers.
Other sorts of handlers you might want to can handle proxies,
authentication, and other common but slightly specialised
situations.
``install_opener`` can be used to make an ``opener`` object the
(global) default opener. This means that calls to ``urlopen`` will use
the opener you have installed.
Opener objects have an ``open`` method, which can be called directly
to fetch urls in the same way as the ``urlopen`` function: there's no
need to call ``install_opener``, except as a convenience.
Basic Authentication
Basic Authentication
====================
====================
To illustrate creating and installing a handler we will use the ``HTTPBasicAuthHandler``. For a more detailed discussion of this subject - including an explanation of how Basic Authentication works - see the `Basic Authentication Tutorial`_.
To illustrate creating and installing a handler we will use the
``HTTPBasicAuthHandler``. For a more detailed discussion of this
subject - including an explanation of how Basic Authentication works -
see the `Basic Authentication Tutorial`_.
When authentication is required, the server sends a header (as well as the 401 error code) requesting authentication. This specifies the authentication scheme and a 'realm'. The header looks like : ``www-authenticate: SCHEME realm="REALM"``.
When authentication is required, the server sends a header (as well as
the 401 error code) requesting authentication. This specifies the
authentication scheme and a 'realm'. The header looks like :
``Www-authenticate: SCHEME realm="REALM"``.
e.g. ::
e.g. ::
www-authenticate: Basic realm="cPanel"
Www-authenticate: Basic realm="cPanel Users"
The client should then retry the request with the appropriate name and password for the realm included as a header in the request. This is 'basic authentication'. In order to simplify this process we can create an instance of ``HTTPBasicAuthHandler`` and an opener to use this handler.
The client should then retry the request with the appropriate name and
password for the realm included as a header in the request. This is
'basic authentication'. In order to simplify this process we can
create an instance of ``HTTPBasicAuthHandler`` and an opener to use
this handler.
The ``HTTPBasicAuthHandler`` uses an object called a password manager to handle the mapping of URIs and realms to passwords and usernames. If you know what the realm is (from the authentication header sent by the server), then you can use a ``HTTPPasswordMgr``. Generally there is only one realm per URI, so it is possible to use ``HTTPPasswordMgrWithDefaultRealm``. This allows you to specify a default username and password for a URI. This will be supplied in the absence of yoou providing an alternative combination for a specific realm. We signify this by providing ``None`` as the realm argument to the ``add_password`` method.
The ``HTTPBasicAuthHandler`` uses an object called a password manager
to handle the mapping of URLs and realms to passwords and
usernames. If you know what the realm is (from the authentication
header sent by the server), then you can use a
``HTTPPasswordMgr``. Frequently one doesn't care what the realm is. In
that case, it is convenient to use
``HTTPPasswordMgrWithDefaultRealm``. This allows you to specify a
default username and password for a URL. This will be supplied in the
absence of yoou providing an alternative combination for a specific
realm. We indicate this by providing ``None`` as the realm argument to
the ``add_password`` method.
The toplevelurl is the first url that requires authentication. This is usually a 'super-url' of any others in the same realm. ::
The top-level URL is the first URL that requires authentication. URLs
"deeper" than the URL you pass to .add_password() will also match. ::
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)
urllib2.install_opener(opener)
# install the opener
# now all calls to urllib2.urlopen use our opener
.. note::
.. note::
In the above example we only supplied our ``HHTPBasicAuthHandler`` to ``build_opener``. By default openers have the handlers for normal situations - ``ProxyHandler``, ``UnknownHandler``, ``HTTPHandler``, ``HTTPDefaultErrorHandler``, ``HTTPRedirectHandler``, ``FTPHandler``, ``FileHandler``, ``HTTPErrorProcessor``. The only reason to explicitly supply these to ``build_opener`` (which chains handlers provided as a list), would be to change the order they appear in the chain.
In the above example we only supplied our ``HHTPBasicAuthHandler``
to ``build_opener``. By default openers have the handlers for
One thing not to get bitten by is that the ``top_level_url`` in the code above *must not* contain the protocol - the ``http://`` part. So if the URL we are trying to access is ``http://www.someserver.com/path/page.html``, then we set : ::
normal situations - ``ProxyHandler``, ``UnknownHandler``,
top_level_url is in fact *either* a full URL (including the 'http:'
# *no* http:// !!
scheme component and the hostname and optionally the port number)
e.g. "http://example.com/" *or* an "authority" (i.e. the hostname,
It took me a long time to track that down the first time I tried to use handlers.
optionally including the port number) e.g. "example.com" or
"example.com:8080" (the latter example includes a port number). The
authority, if present, must NOT contain the "userinfo" component - for
example "joe@password:example.com" is not correct.
Proxies
Proxies
=======
=======
**urllib2** will auto-detect your proxy settings and use those. This is through the ``ProxyHandler`` which is part of the normal handler chain. Normally that's a good thing, but there are occasions when it may not be helpful [#]_. In order to do this we need to setup our own ``ProxyHandler``, with no proxies defined. This is done using similar steps to setting up a `Basic Authentication`_ handler : ::
**urllib2** will auto-detect your proxy settings and use those. This
is through the ``ProxyHandler`` which is part of the normal handler
chain. Normally that's a good thing, but there are occasions when it
may not be helpful [#]_. 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 = urllib2.ProxyHandler({})
>>> proxy_support = urllib2.ProxyHandler({})
>>> opener = urllib2.build_opener(proxy_support)
>>> opener = urllib2.build_opener(proxy_support)
>>> urllib2.install_opener(opener)
>>> urllib2.install_opener(opener)
.. caution::
.. note::
Currently ``urllib2`` *does not* support fetching of ``https`` locations through
Currently ``urllib2`` *does not* support fetching of ``https``
a proxy. This can be a problem.
locations through a proxy. This can be a problem.
Sockets and Layers
Sockets and Layers
==================
==================
The Python support for fetching resources from the web is layered. urllib2 uses the httplib library, which in turn uses the socket library.
The Python support for fetching resources from the web is
layered. urllib2 uses the httplib library, which in turn uses the
socket library.
As of Python 2.3 you can specify how long a socket should wait for a response before timing out. This can be useful in applications which have to fetch web pages. By default the socket module has *no timeout* and can hang. To set the timeout use : ::
As of Python 2.3 you can specify how long a socket should wait for a
response before timing out. This can be useful in applications which
have to fetch web pages. By default the socket module has *no timeout*
and can hang. Currently, the socket timeout is not exposed at the
httplib or urllib2 levels. However, you can set the default timeout
@@ -391,8 +555,8 @@ As of Python 2.3 you can specify how long a socket should wait for a response be
...
@@ -391,8 +555,8 @@ As of Python 2.3 you can specify how long a socket should wait for a response be
Footnotes
Footnotes
===========
===========
.. [#] Possibly some of this tutorial will make it into the standard library docs for versions of Python after 2.4.1.
This document was reviewed and revised by John Lee.
.. [#] You *can* fetch URLs directly with urlopen, without using a request object. It's more explicit, and therefore more Pythonic, to use ``urllib2.Request`` though. It also makes it easier to add headers to your request.
.. [#] For an introduction to the CGI protocol see `Writing Web Applications in Python`_.
.. [#] For an introduction to the CGI protocol see `Writing Web Applications in Python`_.
.. [#] Like Google for example. The *proper* way to use google from a program is to use PyGoogle_ of course. See `Voidspace Google`_ for some examples of using the Google API.
.. [#] Like Google for example. The *proper* way to use google from a program is to use PyGoogle_ of course. See `Voidspace Google`_ for some examples of using the Google API.
.. [#] Browser sniffing is a very bad practise for website design - building sites using web standards is much more sensible. Unfortunately a lot of sites still send different versions to different browsers.
.. [#] Browser sniffing is a very bad practise for website design - building sites using web standards is much more sensible. Unfortunately a lot of sites still send different versions to different browsers.