How to catch socket timeout?

Peter Otten __peter__ at web.de
Sun Sep 21 12:53:02 EDT 2003


Alan Kennedy wrote:

> Hmmm.
> 
> What is wrong with the following code? It seems to do what you need:
> 
> #=======================================================
> from urllib2 import urlopen
> import socket
> import sys
> 
> slowurl = "http://127.0.0.1/cgi-bin/longWait.py?wait=10"
> socket.setdefaulttimeout(1)
> 
> try:
>     data = urlopen(slowurl)
>     data.read()
> except socket.error:
>     errno, errstr = sys.exc_info()[:2]
>     if errno == socket.timeout:
>         print "There was a timeout"
>     else:
>         print "There was some other socket error"
> #==========================================================

You are right. I did not read the traceback carefully.

Peter






More information about the Python-list mailing list