timeout
中的urllib2.urlopen
参数似乎不一致。这是为什么?
我在下面的一些示例网址上测试了参数,测量了超时之前通话的总时间。有些页面是准确的,有些则不是。这是urllib2
的错误吗?任何人都可以解释为什么会这样吗?
下面是我用来测试超时功能的一些示例代码:
import datetime, urllib2, logging, pprint
timeout_sites = [
"http://10.255.255.1", # Unreachable
"http://example.com:81", # No DNS name
"http://google.com:81", # Closed port
"http://httpbin.org/delay/10" # Waits 10 secs, then responds
]
# Calculate the timeout times and put them in here.
timeouts = {}
for url in timeout_sites:
timeouts[url] = []
for t in range(0, 5, 1):
# Save the start time.
start = datetime.datetime.utcnow()
try:
urllib2.urlopen(url, timeout = t)
except Exception as e:
# We're expecting a timeout exception. Measure the total time.
ttime = (datetime.datetime.utcnow() - start).total_seconds()
timeouts[url].append((t, ttime))
logging.info("%s: Timeout=%s, Actual=%s (e=%s %s)"%(url, t, ttime, type(e), e))
else:
raise Exception("Expecting itmeout")
pprint.pprint(timeouts)
我希望测量的超时时间相对接近指定的时间,但在某些情况下它似乎会偏离两倍:
{'http://10.255.255.1': [(0, 0.001),
(1, 1.0),
(2, 2.002),
(3, 3.001),
(4, 4.001)],
'http://example.com:81': [(0, 0.003),
(1, 2.002),
(2, 4.003),
(3, 6.003),
(4, 8.003)],
'http://google.com:81': [(0, 0.033),
(1, 2.002),
(2, 4.003),
(3, 6.002),
(4, 8.002)],
'http://httpbin.org/delay/10': [(0, 0.132),
(1, 1.029),
(2, 2.033),
(3, 3.031),
(4, 4.037)]}