I/O 错误(套接字错误):[Errno 111] 连接被拒绝 [英] I/O error(socket error): [Errno 111] Connection refused

查看:53
本文介绍了I/O 错误(套接字错误):[Errno 111] 连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序使用 urllib 定期获取一个 url,我看到断断续续的错误如:

I have a program that uses urllib to periodically fetch a url, and I see intermittent errors like :

I/O 错误(套接字错误):[Errno 111] 连接被拒绝.

I/O error(socket error): [Errno 111] Connection refused.

它在 90% 的情况下都可以工作,但其他 10% 的情况下它会失败.如果在失败后立即重试获取,则成功.我无法弄清楚为什么会这样.我试着看看是否有任何端口可用,它们是.任何调试想法?

It works 90% of the time, but the othe r10% it fails. If retry the fetch immediately after it fails, it succeeds. I'm unable to figure out why this is so. I tried to see if any ports are available, and they are. Any debugging ideas?

有关其他信息,堆栈跟踪是:

For additional info, the stack trace is:

File "/usr/lib/python2.6/urllib.py", line 203, in open 
    return getattr(self, name)(url)

File "/usr/lib/python2.6/urllib.py", line 342, in open_http
    h.endheaders()

File "/usr/lib/python2.6/httplib.py", line 868, in endheaders
    self._send_output()

File "/usr/lib/python2.6/httplib.py", line 740, in _send_output
    self.send(msg)

File "/usr/lib/python2.6/httplib.py", line 699, in send
    self.connect()

File "/usr/lib/python2.6/httplib.py", line 683, in connect
    self.timeout)

File "/usr/lib/python2.6/socket.py", line 512, in create_connection
    raise error, msg

编辑 - 谷歌搜索不是很有帮助,我从中得到的是服务器我从有时拒绝连接中获取,我如何验证它不是我的代码中的错误事实确实如此?

Edit - A google search isn't very helpful, what I got out of it is that the server I'm fetching from sometimes refuses connections, how can I verify its not a bug in my code and this is indeed the case?

推荐答案

使用像 Wireshark 这样的数据包嗅探器来看看会发生什么.您需要看到带有 SYN 标志的数据包传出、带有 SYN+ACK 标志的传入和带有 ACK 标志的传出.之后,该端口在本地被认为是开放的.

Use a packet sniffer like Wireshark to look at what happens. You need to see a SYN-flagged packet outgoing, a SYN+ACK-flagged incoming and then a ACK-flagged outgoing. After that, the port is considered open on the local side.

如果您只看到第一个数据包并且在等待几秒钟后出现错误消息,则另一方根本没有响应(例如:拔下电缆、服务器过载、错误引导的数据包被丢弃)并且您的本地网络堆栈中止连接尝试.如果您看到 RST 数据包,则主机实际上拒绝连接.如果您看到ICMP 端口无法访问"或主机无法访问的数据包,则防火墙或目标主机会通知您该端口实际上已关闭.

If you only see the first packet and the error message comes after several seconds of waiting, the other side is not answering at all (like in: unplugged cable, overloaded server, misguided packet was discarded) and your local network stack aborts the connection attempt. If you see RST packets, the host actually denies the connection. If you see "ICMP Port unreachable" or host unreachable packets, a firewall or the target host inform you of the port actually being closed.

当然,您不能期望服务始终可用(考虑您和数据之间的所有故障点),因此您应该稍后再试.

Of course you cannot expect the service to be available at all times (consider all the points of failure in between you and the data), so you should try again later.

这篇关于I/O 错误(套接字错误):[Errno 111] 连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆