当PUT使用请求时,SSLError(读操作超时)而不是超时

时间:2013-12-10 20:18:43

标签: rest python-2.7 python-requests

我正在尝试使用Requests包对一台设备执行各种REST请求。 它正在工作,但是在一些请求上,比如PUT,我得到了一个SSLError,而不是我指定的超时。

我将代码设置为重试,最多5次,每次加倍超时(1,2,4,8,16,32),如果我将SSLError视为超时,那么它最终会通过。这是一个示例,您可以看到SSLError正在以超时间隔的速度发生:

2013-12-10 19:41:13.208 22294 DEBUG client [-] PUT: Request for https://192.168.200.20/api/v1/global/host-name headers {'content-type': 'application/json', 'Accept': 'application/json', 'X-auth-token': u'...omitted...'} payload {'host-name': 'TestHost'}
2013-12-10 19:41:13.209 22294 INFO requests.packages.urllib3.connectionpool [-] Starting new HTTPS connection (1): 192.168.200.20
2013-12-10 19:41:14.253 22294 ERROR client [-] EXCEPTION The read operation timed out  <<< SSL Error
2013-12-10 19:41:14.255 22294 INFO requests.packages.urllib3.connectionpool [-] Starting new HTTPS connection (1): 192.168.200.20
2013-12-10 19:41:16.301 22294 ERROR client [-] EXCEPTION The read operation timed out
2013-12-10 19:41:16.302 22294 INFO requests.packages.urllib3.connectionpool [-] Starting new HTTPS connection (1): 192.168.200.20
2013-12-10 19:41:20.346 22294 ERROR client [-] EXCEPTION The read operation timed out
2013-12-10 19:41:20.348 22294 INFO requests.packages.urllib3.connectionpool [-] Starting new HTTPS connection (1): 192.168.200.20
2013-12-10 19:41:25.845 22294 DEBUG requests.packages.urllib3.connectionpool [-] "PUT /api/v1/global/host-name HTTP/1.1" 204 0 _make_request /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:289
2013-12-10 19:41:25.846 22294 DEBUG client [-] PUT: Completed [204]

正如您所看到的,EXCEPTION日志消息是SSLError错误,但是当它们映射到我指定的超时时它们是超时。

在python中,我正在为调用做这个(它已经散布,所以这里是它的要点):

url = ('https://%(host)s/api/v1/%(resource)s' %
       {'host': self.host, 'resource': resource})
...
response = self._request(method, url, try_num, timeout=timeout,
                         headers=headers, data=payload)
...
def _request(self, method, url, attempt, **kwargs):
    ...
    try:
        response = requests.request(method, url, verify=False, **kwargs)
    except Timeout:
        self.status = wexc.HTTPRequestTimeout.code
        LOG.debug(_("%(method)s: Request timeout" ...)
    except SSLError as se:
        LOG.error("EXCEPTION %s", se)
        self.status = wexc.HTTPRequestTimeout.code

如果我不处理SSL错误,那么它会被捕获为ConnectionError。知道我可能做错了吗?

我有这个hack,但想知道为什么我会收到SSL错误。

1 个答案:

答案 0 :(得分:1)

谢谢Lukasa!你的问题促使我调查版本。我安装了请求2.1.0,现在我看到Timeout异常,而不是SSLError异常,这是我期望看到的。