我正在使用 python 套接字连接到服务器,但有时我得到这个:
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
当我调用socket.connect方法时
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((self._ipv4address, host_port))
try:
s.connect((dest_ip, dest_port))
except:
raise
为什么我会看到此错误?我该如何解决这个问题?
最佳答案
您不需要绑定(bind)套接字(除非远程服务器期望传入套接字) - 这实际上是连接的要求的情况极为罕见。
不要使用套接字打开网站,而是使用 urllib2或mechanize如果您需要摆弄表格。它们管理 cookie、 session 、页面状态等。更容易。
此外,如果您无法连接..不要放弃!再试一次,有些网站可能无法响应。有些人可能会暂时没有反应,具体取决于 - 更好地处理它。不要只是引发错误,而是用 exponential backoff 包装您的连接方法。装饰器。
关于python套接字错误10060,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456422/