【发布时间】:2012-08-22 03:37:21
【问题描述】:
我有以下代码已经运行了大约一年:
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
【问题讨论】:
-
在 linux 终端上尝试
telnet somewhere.com 443并检查其名称解析失败或连接失败的位置。 -
使用 try 和 catch 语句,然后在 catch 语句中打印错误可能会显示更多信息。
-
telnet 告诉你的 IP 地址总是一样吗?
-
是的,每个telnet请求的IP地址都是一样的
-
Server Fault 有一个关于 Connection Refused 的规范问题。
标签: python networking