简体   繁体   English

python请求ssl握手失败

[英]python requests ssl handshake failure

Every time I try to do: 每次我尝试做:

requests.get('https://url') 

I got this message: 我收到此消息:

import requests
>>> requests.get('https://reviews.gethuman.com/companies') 
Traceback (most recent call last): 
   File "<stdin>", line 1, in <module> 
   File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get 
    return request('get', url, **kwargs) 
   File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request 
    return session.request(method=method, url=url, **kwargs)    
   File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 455, in request 
    resp = self.send(prep, **send_kwargs) 
   File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 558, in send 
    r = adapter.send(request, **kwargs) 
   File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 385, in send 
    raise SSLError(e) requests.exceptions.SSLError: [Errno 1]
_ssl.c:510: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure

I tried everything: 我尝试了一切:

  • update my requests 更新我的要求
  • update my ssl 更新我的SSL

but nothing changes. 但没有任何变化。

I am using Python 2.7.6, can't change this. 我正在使用Python 2.7.6,无法更改此设置。

On OSX, using python 2.7.10 / requests 2.9.1 I only had to to install requests using its security setup: 在OSX上,使用python 2.7.10 / requests 2.9.1,我只requests使用其安全性设置安装requests

pip install requests[security]

This installs pyOpenSSL , ndg-httpsclient and pyasn1 . 这将安装pyOpenSSLndg-httpsclientpyasn1 https://github.com/kennethreitz/requests/blob/master/setup.py#L70 https://github.com/kennethreitz/requests/blob/master/setup.py#L70

I resolve the problem in the end i updated my ubuntu from 14.04 to 14.10 and the problem was solved 我最终解决了问题,将Ubuntu版本从14.04更新到了14.10,问题已解决。

but in the older version of ubuntu and python I install those lib and it seems to fix all my problems 但是在旧版本的ubuntu和python中,我安装了这些lib,看来可以解决所有问题

sudo apt-get install python-dev libssl-dev libffi-dev
sudo pip2.7 install -U pyopenssl==0.13.1 pyasn1 ndg-httpsclient

if you don`t have pip2.7 installed you can use pip instead 如果您没有安装pip2.7,则可以改用pip

If you are not able to upgrade your Python version to 2.7.9 then downgrade you requests package to 2.5.3. 如果您无法将Python版本升级到2.7.9,则将其降级到2.5.3。 That worked for me. 那对我有用。

sudo pip install requests==2.5.3

Edit 编辑

You can also install requests with security extension 您还可以安装带有安全扩展的请求

pip install requests[security]
import requests

#Add support for all cipher suites
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS='ALL'
#Exclude use of pyopenssl (pyopenssl module may need to be installed first $pip install pyopenssl)
requests.packages.urllib3.contrib.pyopenssl.extract_from_urllib3()

I am using python 2.7.6 cant change it. 我正在使用python 2.7.6不能更改它。

Sorry for you then. 不好意思 The server you are trying to reach requires (SNI) Server Name Indication and will cause a handshake failure if the client is not using this SNI extension.. Support for SNI was only added with python 2.7.9 so it looks like you are out of luck. 您尝试连接的服务器需要(SNI)服务器名称指示 ,如果客户端未使用此SNI扩展 ,则将导致握手失败。.仅在python 2.7.9中添加了对SNI的支持,因此您似乎已无法使用运气。

after trying all the above solutions and going down quite the rabbit hole, the thing that eventually cracked the case was simply trying to import pyopenssl from the urllib3 package like so: 在尝试了上述所有解决方案并陷入困境之后,最终使案件破裂的只是尝试从urllib3包中导入pyopenssl,如下所示:

from urllib3.contrib import pyopenssl

which led me right to the error (missing the cryptography package) 这使我正确地犯了错误(缺少cryptography软件包)

I kept getting this error message when connecting to an old website with obsolete HTTPS. 当使用过时的HTTPS连接到旧网站时,我一直收到此错误消息。

In the end I had to set the Protocol AND the Ciphers manually with an adapter for the request to work. 最后,我必须使用适配器手动设置协议和密码,以使请求正常工作。

What worked for me - 什么对我有用-

  1. go to a diagnostics site like this one https://www.ssllabs.com/ssltest/analyze.html 转到这样的诊断站点https://www.ssllabs.com/ssltest/analyze.html
  2. find out what protocol the website uses (TLS1.0, TLS1.2, SSL3.0 (only?) ....) 找出网站使用的协议(TLS1.0,TLS1.2,SSL3.0(仅?)...。)
  3. find out what obsolete ciphers suite are used 找出使用了哪些过时的密码套件
  4. from 3) translate cipher name to the the openssl name from a list like https://www.openssl.org/docs/man1.1.0/apps/ciphers.html or http://testssl.sh/openssl-rfc.mapping.html to use in code 从3开始)将密码名称从https://www.openssl.org/docs/man1.1.0/apps/ciphers.htmlhttp://testssl.sh/openssl-rfc.mapping之类的列表中转换为openssl名称.html在代码中使用

then in requests create an adapter with what you have researched above 然后在请求中使用上面研究的内容创建一个适配器

import requests, ssl
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
from requests.packages.urllib3.util import ssl_

class OldInsecureWebsiteAdapter(HTTPAdapter):
    def __init__(self, **kwargs):
        super(OldInsecureWebsiteAdapter, self).__init__(**kwargs)
    def init_poolmanager(self, *pool_args, **pool_kwargs):
        """
        my website uses TLS1.0 ONLY (from step 2) 
        and several obsolete ciphers (from step 3/4 I only picked one)
        """
        context = ssl_.create_urllib3_context(ssl.PROTOCOL_TLSv1, ciphers="DES-CBC3-SHA")

        """
        website does not support anything else so exclude all other protocols
        """
        context.options |= ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_3 | ssl.OP_NO_TLSv1_2 
        context.options |= ssl.OP_NO_SSLv3 | ssl.OP_NO_SSLv2

        self.poolmanager = PoolManager(*pool_args,
                                   ssl_context=context,
                                   **pool_kwargs)

now I mount the adapter to the requests object 现在我将适配器安装到请求对象

    url = "https://yourwebsite.gov.whocares"
    session = requests.session()
    session.mount(url, OldInsecureWebsiteAdapter())
    result = session.get(url) 
    #no more ssl3 handshake error...

Running python 2.7.13. 运行python 2.7.13。 I tried the recommended solutions here, which ended up installing the following: 我在这里尝试了推荐的解决方案,最终安装了以下内容:

  • pip install requests[security] (installed asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.4.2 idna-2.7 ipaddress-1.0.22 ndg-httpsclient-0.5.1 pyOpenSSL-18.0.0 pyasn1-0.4.4 pycparser-2.19) pip install requests[security] (已安装asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.4.2 idna-2.7 ipaddress-1.0.22 ndg-httpsclient-0.5.1 pyOpenSSL-18.0.0 pyasn1-0.4.4 pycparser- 2.19)

no help, then tried 没有帮助,然后尝试

  • pip install -U requests (installed certifi-2018.10.15 chardet-3.0.4 requests-2.20.1 urllib3-1.24.1) pip install -U requests (已安装certifi-2018.10.15 chardet-3.0.4请求-2.20.1 urllib3-1.24.1)

no help. 没有帮助 Finally tried 终于尝试了

  • pip install -U httplib2 (httplib2-0.12.0) pip install -U httplib2 (httplib2-0.12.0)

which resolved the problem. 解决了问题。 Previously I was running httplib2==0.9.2 以前我正在运行httplib2 == 0.9.2

You just need to upgrade your requests. 您只需要升级您的请求。

pip install requests --upgrade pip安装请求--upgrade

You can still use python 2.7. 您仍然可以使用python 2.7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM