how to time-out gracefully while downloading with python

后端 未结 4 864
面向向阳花
面向向阳花 2021-02-08 11:20

I\'m downloading a huge set of files with following code in a loop:

try:
    urllib.urlretrieve(url2download, destination_on_local_filesystem)
except KeyboardInt         


        
相关标签:
4条回答
  • 2021-02-08 11:47

    Try:

    import socket

    socket.setdefaulttimeout(30)

    0 讨论(0)
  • 2021-02-08 12:02

    There's a discussion of this here. Caveats (in addition to the ones they mention): I haven't tried it, and they're using urllib2, not urllib (would that be a problem for you?) (Actually, now that I think about it, this technique would probably work for urllib, too).

    0 讨论(0)
  • 2021-02-08 12:10

    This question is more general about timing out a function: How to limit execution time of a function call in Python

    I've used the method described in my answer there to write a wait for text function that times out to attempt an auto-login. If you'd like similar functionality you can reference the code here:

    http://code.google.com/p/psftplib/source/browse/trunk/psftplib.py

    0 讨论(0)
  • 2021-02-08 12:13

    If you're not limited to what's shipped with python out of the box, then the urlgrabber module might come in handy:

    import urlgrabber
    urlgrabber.urlgrab(url2download, destination_on_local_filesystem,
                       timeout=30.0)
    
    0 讨论(0)
提交回复
热议问题