Timeout a file download with Python urllib?

前端 未结 3 683
一生所求
一生所求 2020-12-09 19:24

Python beginner here. I want to be able to timeout my download of a video file if the process takes longer than 500 seconds.

import urllib
try:
   urllib.ur         


        
3条回答
  •  我在风中等你
    2020-12-09 19:43

    Although urlretrieve does not have this feature, you can still set the default timeout (in seconds) for all new socket objects.

    import socket
    import urllib    
    
    socket.setdefaulttimeout(15)
    
    try:
       urllib.urlretrieve ("http://www.videoURL.mp4", "filename.mp4")
    except Exception as e:
       print("error")
    

提交回复
热议问题