Python - urllib2 超时
我有一些代码,下面是我的代码片段:
opener = urllib2.build_opener(redirect_handler.MyHTTPRedirectHandler())
opener.addheaders = [('Accept-encoding', 'gzip')]
fetch_timeout = 12
self.response = opener.open(url, timeout=fetch_timeout)
不过,这段代码在超时之前还是要等60秒左右……有没有什么线索?
3 个回答
0
看看这个 OpenerDirector 类和 urllib2.install_opener() 方法。
2
你在用哪个版本?这个功能是在2.6版本中添加的。
这个方法是
urllib2.urlopen(url[, data][, timeout])
你能试着提供一下
self.response = opener.open(url, None, fetch_timeout)
是的,对于其他所有情况,你仍然可以使用socket模块来设置socket的超时时间。
3
我猜你可能需要设置一下套接字的超时时间。
import socket
default_timeout = 12
socket.setdefaulttimeout(default_timeout)