【发布时间】:2014-04-26 16:53:18
【问题描述】:
linux下的python 2.7.3:尝试使用超时参数时出现奇怪的行为
from urllib2 import urlopen, Request, HTTPError, URLError
url = "http://speedtest.website-solution.net/speedtest/random350x350.jpg"
try:
#f = urlopen(url, timeout=30) #never works - always times out
f = urlopen(url) #always works fine, returns after < 2 secs
print("opened")
f.close()
print("closed")
except IOError as e:
print(e)
pass
编辑:
深入研究,似乎级别较低..以下代码有同样的问题:
s = socket.socket()
s.settimeout(30)
s.connect(("speedtest.website-solution.net", 80)) #times out
print("opened socket")
s.close()
它在 socks 代理后面运行。使用tsocks python test.py 运行。想知道这是否会因为某种原因搞砸套接字超时? timeout=None 工作正常,这似乎很奇怪。
【问题讨论】:
-
+1 表示简洁、完整的示例程序。调用
urlopen,您的程序可以在我的PC 上完美运行。我有 Ubuntu 13.10,Python 2.7.5。您使用的是什么操作系统? -
没有好主意;当你说它总是超时时,它会在你指定的时间之后超时吗?如果设置 timeout(100) 需要 100 秒才能超时?
-
@TessellatingHeckler 是的 - 完全正确