Python请求的重试次数超过了url的最大重试次数

2022-03-16 00:00:00 python python-requests teamcity

问题描述

我在TeamCity上有一个使用自定义python脚本的构建步骤。当我尝试运行生成时,收到以下错误:

[20:57:12][Step 1/3] Traceback (most recent call last):
[20:57:12][Step 1/3]   File "/amirsys/teamcity-agent/lastBuildStatus.py", line 24, in <module>
[20:57:12][Step 1/3]     main()
[20:57:12][Step 1/3]   File "/amirsys/teamcity-agent/lastBuildStatus.py", line 13, in main
[20:57:12][Step 1/3]     build = getLastBuild(buildId)
[20:57:12][Step 1/3]   File "/amirsys/teamcity-agent/teamcity.py", line 204, in getLastBuild
[20:57:12][Step 1/3]     return requests.get(url, auth=auth)
[20:57:12][Step 1/3]   File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
[20:57:12][Step 1/3]     return request('get', url, **kwargs)
[20:57:12][Step 1/3]   File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
[20:57:12][Step 1/3]     return session.request(method=method, url=url, **kwargs)
[20:57:12][Step 1/3]   File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 335, in request
[20:57:12][Step 1/3]     resp = self.send(prep, **send_kwargs)
[20:57:12][Step 1/3]   File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 438, in send
[20:57:12][Step 1/3]     r = adapter.send(request, **kwargs)
[20:57:12][Step 1/3]   File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 327, in send
[20:57:12][Step 1/3]     raise ConnectionError(e)
[20:57:12][Step 1/3] requests.exceptions.ConnectionError: HTTPSConnectionPool(host='teamcity.amirsys-int.com', port=443): Max retries exceeded with url: /httpAuth/app/rest/builds/buildType:bt262,count:1 (Caused by <class 'socket.error'>: [Errno 110] Connection timed out)

我已经研究并找到了一个答案,我认为它可能在这里:Max retries exceeded with URL。他们建议使用:

from time import sleep

要分散请求间隔吗?这是我需要做的吗?

我不明白我怎么会发送太多请求,因为我的脚本只发出这个简单的请求:

def getLastBuild(buildConfigId):
    url = path + 'builds/buildType:' + buildConfigId + ',count:1'
    return requests.get(url, auth=auth)

def main():

    buildId = sys.argv[1]

    build = getLastBuild(buildId)

    if build.status_code == 200:
        result = "SUCCESS"
        print result
    else:
        result = "FAILURE"
        print result

if __name__ == "__main__":
     main()

此请求为什么被拒绝?


解决方案

我终于找到了问题所在。该请求正在通过离开本地主机来访问URL,并且无法"返回",因此连接超时。已通过提供正确的IP地址修复此问题。

相关文章