我有以下代码已经工作了大约一年:
import urllib2
req = urllib2.Request('https://somewhere.com','<Request></Request>')
data = urllib2.urlopen(req)
print data.read()
最近,出现了一些随机错误:
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
<urlopen error [Errno 110] Connection timed out>
失败的痕迹是:
Traceback (most recent call last):
File "test.py", line 4, in <module>
data = urllib2.urlopen(req).read()
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
上述错误是随机发生的,脚本第一次可以成功运行,但第二次运行失败,反之亦然。
我应该怎么做才能调试并找出问题出在哪里? 如何判断端点是否已使用我的请求并返回响应但从未到达我?
使用远程登录
我只是用telnet测试过,有时成功,有时不成功,就像我的Python一样。
成功时:
$ telnet somewhere.com 443
Trying XXX.YY.ZZZ.WWW...
Connected to somewhere.com.
Escape character is '^]'.
Connection closed by foreign host.
在被拒绝的连接上:
$ telnet somewhere.com 443
Trying XXX.YY.ZZZ.WWW...
telnet: Unable to connect to remote host: Connection refused
超时:
$ telnet somewhere.com 443
Trying XXX.YY.ZZZ.WWW...
telnet: Unable to connect to remote host: Connection timed out