Python请求使用url超出最大重试次数

时间:2016-11-01 21:35:51

标签: python teamcity python-requests

我在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()

为什么拒绝此请求?

1 个答案:

答案 0 :(得分:0)

我终于弄明白了问题所在。该请求是通过离开本地主机访问网址而无法返回&#34;所以连接超时了。这是通过提供正确的IP地址来解决的。