Skip to main content
 首页 » 编程设计

尽管超时设置,Python 请求发布超时

2024年12月31日7Free-Thinker

我正在使用 Python Requests模块 (v. 2.19.1) 与 Python 3.4.3,调用远程服务器上的函数生成 .csv 文件以供下载。一般来说,它工作得很好。有一个特定的文件需要 6 分钟以上的时间才能完成,无论我将超时参数设置为什么,我都会在尝试生成该文件的恰好 5 分钟后收到错误消息。

import requests 
s = requests.Session() 
authPayload = {'UserName': 'myloginname','Password': 'password'} 
loginURL = 'https://myremoteserver.com/login/authenticate' 
login = s.post(loginURL, data=authPayload) 
backupURL = 'https://myremoteserver.com/directory/jsp/Backup.jsp' 
payload = {'command': fileCommand} 
headers = {'Connection': 'keep-alive'} 
post = s.post(backupURL, data=payload, headers=headers, timeout=None) 

恰好 5 分钟后超时并出现错误:

File "/usr/lib/python3/dist-packages/requests/adapters.py", line 330, in send timeout=timeout

File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 612, in urlopen raise MaxRetryError(self, url, e)

urllib3.exceptions.MaxRetryError: > HTTPSConnectionPool(host='myremoteserver.com', port=443): Max retries exceeded with url: /directory/jsp/Backup.jsp (Caused by < class 'http.client.BadStatusLine'>: '')

如果我将超时设置为更小的值,比如 5 秒,我会得到一个非常合理的错误:

urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='myremoteserver.com', port=443): Read timed out. (read timeout=5)

如果我从浏览器运行这个过程,它工作正常,所以它看起来不像是远程服务器关闭连接,或者是防火墙或其他中间关闭连接的东西。

请您参考如下方法:

应 OP 的要求发布——我对原始问题的评论指出了相关的 SO 问题

问题的线索在于 http.client.BadStatusLine 错误。

看看下面的相关SO Q & A讨论了代理服务器对 HTTP 请求和响应的影响。