Python:SSLError使用surveymonkey.com的请求

时间:2015-07-30 17:56:00

标签: python ssl openssl

Python版本:Python 2.7.6

操作系统:OSX 10.10.3

我正在尝试requestshttps://www.surveymonkey.com/

>>> import requests
>>> requests.get('https://www.surveymonkey.com/')

但是我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/api.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno bad handshake] [('SSL routines', 'SSL23_GET_SERVER_HELLO', 'sslv3 alert handshake failure')]

我对“https://www.google.com

没有同样的错误
>>> import requests
>>> requests.get('https://www.google.com')
<Response [200]>

这是否意味着surveymonkey的ssl存在问题?

Python版本:2.7.10

>>> requests.get('https://www.surveymonkey.com/mp/legal/which-terms-apply')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/api.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)

更新: 我已经尝试过Python 2.7.6,Python 2.7.9,Python 2.7.10

OS:OSX 10.10.3 with OpenSSL 1.0.2d 2015年7月9日。请求版本2.4.3,2.5.3,2.7.0。

我试过configure the ssl protocol. Python 2.7.10 / Requests 2.7.0以下是代码:

import ssl
import requests

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager


class Ssl3HttpAdapter(HTTPAdapter):
    """"Transport adapter" that allows us to use SSLv3."""

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_SSLv3)

s = requests.Session()
s.mount('https://', Ssl3HttpAdapter())
url = 'https://www.surveymonkey.com/'
print s.get(url)

我仍然收到错误:

Traceback (most recent call last):
  File "redirect.py", line 29, in <module>
    print s.get(url)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/sessions.py", line 469, in get
    return self.request('GET', url, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/sessions.py", line 569, in send
    r = adapter.send(request, **kwargs)
  File "/Users/jasony/Documents/python/scripts2/env/lib/python2.7/site-packages/requests/adapters.py", line 420, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)

2 个答案:

答案 0 :(得分:4)

我无法在我的机器上重现这一点(Py 3.4.2 / 2.7.9,请求2.4.3,请求2.7.0,OpenSSL 1.0.1k,Debian Jessie)。我在一些旧OpenSSL版本的机器上看到了类似的消息。

您可以尝试升级Python / OpenSSL(老版本的Python附带旧的OpenSSL版本),或者通过使用OpenSSL命令行工具进行测试并更改选项以深入挖掘来深入挖掘。告诉我们您使用的操作系统和Python版本可能会有所帮助。

openssl s_client -connect www.surveymonkey.com:443

请参阅SSL实验室,以人性化的格式获取受支持的服务器TLS版本和密码。 https://www.ssllabs.com/ssltest/analyze.html?d=www.surveymonkey.com

根据建议您可以尝试强制某个TLS版本或更改支持的密码列表。据我所知,似乎没有很好的方法可以通过API影响密码。

由于它适用于某些人,而不适用于其他人,我们可能希望比较不同版本中使用的默认密码。将其更改为ALL应该可能使其工作,但不是很安全,因此应该找出哪个限制是罪魁祸首。 SSL实验室网站可以帮助您。

请参阅以下有关如何更改默认筹码人名单的代码:

import requests.packages.urllib3.util.ssl_
print(requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS)
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL'

答案 1 :(得分:0)

两种方法:

  1. import requests.packages.urllib3.util.ssl_ requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL'

  2. $ pip install requests[security]