且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python 处理 socket.error: [Errno 104] 对等方重置连接

更新时间:2021-10-31 21:44:22

由对等方重置连接"是 TCP/IP 等价物,相当于将电话重新挂断.这比仅仅不回答、留下一个悬而未决更有礼貌.但这不是真正礼貌的 TCP/IP 对话者所期望的 FIN-ACK.(来自其他 SO 答案)

所以你无能为力,这是服务器的问题.

So you can't do anything about it, it is the issue of the server.

但是您可以使用 try .. except 块来处理该异常:

But you could use try .. except block to handle that exception:

from socket import error as SocketError
import errno

try:
    response = urllib2.urlopen(request).read()
except SocketError as e:
    if e.errno != errno.ECONNRESET:
        raise # Not error we are looking for
    pass # Handle error here.