Python 如何解决10054错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27333671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:40:39  来源:igfitidea点击:

How to solve the 10054 error

pythonconnectionresponse

提问by Deep_fox

I'm using an app provided by some website to collect some data from the website periodly, say, 30s a time. The returned response is then recorded in database.

我正在使用某个网站提供的应用程序定期从该网站收集一些数据,例如一次 30 秒。然后将返回的响应记录在数据库中。

I use the requests modular by import requests and write codes to catch Exception. The codes for the main function are as following:

我通过导入请求使用请求模块化并编写代码来捕获异常。main函数的代码如下:

def get_response(self):
    try:
       response = requests.get(self.request_url)
       if response.status_code == 200:
          return response.json()
       except Exception as e:
          msg = "Exception is:\n %s \n" % e
          print msg

The above function works quite well for the first several hours. The function can also recover from some exceptions like: ('Connection aborted.', BadStatusLine("''",))
or ('Connection aborted.', error(10053, '')) It omits the exception (by recording a Null in database) and continues to get the response of next period.

上述功能在前几个小时内运行良好。该函数还可以从一些异常中恢复,例如: ('Connection aborted.', BadStatusLine("''",))
或 ('Connection aborted.', error(10053, '')) 它省略了异常(通过记录一个数据库中为空)并继续获取下一个周期的响应。

However, the function stops working when encoutering a 10054 error.

但是,该函数在遇到 10054 错误时停止工作。

Exception is:
 ('Connection aborted.', error(10054, '')) 

I check the database to find that all the response is Null after the time that the 10054 error comes. I firstly guess that the website may breakdown, thus no response is received. But when I manually restart the function, it starts to get response again. So that's no realated with breakdown of the website.

我查看了数据库,发现10054错误来了之后,所有的响应都是Null。我首先猜测网站可能会崩溃,因此没有收到任何回复。但是当我手动重新启动该功能时,它又开始得到响应。所以这与网站的故障无关。

I search in stackoverflow and find: Errno 10054 An existing connection was forcibly closed by the remote host. But I don't know how to resolve it.

我在 stackoverflow 中搜索,发现: Errno 10054 An existing connection was forceed close by the remote host。但我不知道如何解决它。

Could you please provide some solotion to this problem?(ideally speaking) or provide some solution to restart the function without manually restarting? (It looks like once I restart the funtion and it works again.)

您能否为此问题提供一些解决方案?(理想情况下)或提供一些解决方案来重新启动该功能而无需手动重新启动?(看起来一旦我重新启动该功能,它就会再次运行。)

Thanks in advance.

提前致谢。

回答by tdelaney

The web server actively rejected your connection. That's usually because it is congested, has rate limiting or thinks that you are launching a denial of service attack. If you get this from a server, you should sleep a bit before trying again. In fact, if you don't sleep before retry, you are a denial of service attack. The polite thing to do is implement a progressive sleep of, say, (1,2,4,8,16,32) seconds.

Web 服务器主动拒绝了您的连接。这通常是因为它拥塞、有速率限制或认为您正在发起拒绝服务攻击。如果您从服务器获取此信息,则应在重试之前稍作休息。事实上,如果你在重试之前不睡觉,你就是拒绝服务攻击。礼貌的做法是实现(1,2,4,8,16,32)秒的渐进睡眠。