Read timeout using either urllib2 or any other http library

后端 未结 8 961
花落未央
花落未央 2020-11-29 05:36

I have code for reading an url like this:

from urllib2 import Request, urlopen
req = Request(url)
for key, val in headers.items():
    req.add_header(key, va         


        
8条回答
  •  星月不相逢
    2020-11-29 05:59

    I'd expect this to be a common problem, and yet - no answers to be found anywhere... Just built a solution for this using timeout signal:

    import urllib2
    import socket
    
    timeout = 10
    socket.setdefaulttimeout(timeout)
    
    import time
    import signal
    
    def timeout_catcher(signum, _):
        raise urllib2.URLError("Read timeout")
    
    signal.signal(signal.SIGALRM, timeout_catcher)
    
    def safe_read(url, timeout_time):
        signal.setitimer(signal.ITIMER_REAL, timeout_time)
        url = 'http://uberdns.eu'
        content = urllib2.urlopen(url, timeout=timeout_time).read()
        signal.setitimer(signal.ITIMER_REAL, 0)
        # you should also catch any exceptions going out of urlopen here,
        # set the timer to 0, and pass the exceptions on.
    

    The credit for the signal part of the solution goes here btw: python timer mystery

提交回复
热议问题