timeout on urllib.urlopen?

Steffen Ries steffen at cyberus.ca
Mon Apr 26 07:59:50 EDT 1999


jam <jam at newimage.com> writes:

> On Mon, Apr 26, 1999 at 04:48:44AM +0000, Kevin L wrote:
> > 
> > I'm trying to use urllib.urlopen() on a big list of urls, some of which are
> > dead (they don't return a 404, just no response). And the function just waits.
> > Is there any way to specify a timeout period for this function? thanks,

.... 
> attached, please find a short lightly tested module that might do what you
> are looking for.. please let me know if this is what you need. it's a piece
> of code I wrote for a larger application, and it seems to get the job done
> nicely. suggestions for optimizations, etc, accepted.

I used once SIGALRM to force a timeout. Maybe somebody could comment
on that approach?


/steffen

--8<----8<----8<----8<--
import signal

def alarmHandler(*args):
    """
    signal handler for SIGALRM, just raise an exception
    """
    raise "TimeOut"

....
    signal.signal(signal.SIGALRM, alarmHandler)
    try:
        # set timeout
        signal.alarm(120)

        #... urllib.urlretrieve pages

    except "TimeOut":
        # some error handling
    signal.alarm(0)
--8<----8<----8<----8<--
-- 
steffen at cyberus.ca		<> Gravity is a myth -- the Earth sucks!




More information about the Python-list mailing list