How to prevent the script from stopping before it should

Steve Holden steve at holdenweb.com
Mon Jan 17 18:50:06 CET 2005


wittempj at hotmail.com wrote:

> #import urllib, sys
> #pages = ['http://www.python.org', 'http://xxx']
> #for i in pages:
> #   try:
> #        u = urllib.urlopen(i)
> #        print u.geturl()
> #    except Exception, e:
> #        print >> sys.stderr, '%s: %s' % (e.__class__.__name__, e)
> will print an error if a page fails opening, rest opens fine
> 
More generally you may wish to use the timeout features of TCP sockets. 
These were introduced in Python 2.3, though Tim O'Malley's module 
"timeoutsocket" (which was the inspiration for the 2.3 upgrade) was 
available for earlier versions.

You will need to import the socket module and then call 
socket.setdefaulttimeout() to ensure that communication with 
non-responsive servers results in a socket exception that you can trap.

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list