## 8 Sockets and Layers 

The Python support for fetching resources from the web is layered. urllib uses the http.client 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. Currently, the socket timeout is not exposed at the http.client or urllib.request levels. However, you can set the default timeout globally for all sockets using 

import socket import urllib.request # timeout in seconds timeout =10 socket.setdefaulttimeout(timeout) # this call to urllib.request.urlopen now uses the default timeout # we have set in the socket module req =urllib.request.Request('http://www.voidspace.org.uk ') response =urllib.request.urlopen(req) 

## 9 Footnotes 

This document was reviewed and revised by John Lee. 

