返回顶部
首页 > 资讯 > 后端开发 > Python >关于EOF occurred in v
  • 194
分享到

关于EOF occurred in v

EOFoccurred 2023-01-31 08:01:56 194人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

关于EOF occurred in violation of protocol (_ssl.c:661)的解决办法 直接上代码(python版本2.7)`import requestsr = requests.get('https://w

关于EOF occurred in violation of protocol (_ssl.c:661)的解决办法

直接上代码(python版本2.7)
`import requests
r = requests.get('https://www.zjkill.com/news/')
r.encoding = 'utf8'
print r.text
print r.status_code,r.url`
报错

requests.exceptions.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)
经过一番查找,发现是requests不在支持3DES stream cipher。
下面是解决办法,注意ruquests版本2.11及以下的
from requests.adapters import HttpAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl

class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1)
s = requests.Session()
s.mount('https://', MyAdapter())
r = requests.get('https://www.zjkill.com/news/')
r.encoding = 'utf8'
print r.text

如果requests版本是2.12及以上

import requests
                from requests.adapters import HTTPAdapter
                from requests.packages.urllib3.util.ssl_ import create_urllib3_context
                CIPHERS = (
                    'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
                    'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
                    '!eNULL:!MD5'
                )

                class DESAdapter(HTTPAdapter):
                    """
                    A TransportAdapter that re-enables 3DES support in Requests.
                    """

                    def init_poolmanager(self, *args, **kwargs):
                        context = create_urllib3_context(ciphers=CIPHERS)
                        kwargs['ssl_context'] = context
                        return super(DESAdapter, self).init_poolmanager(*args, **kwargs)

                    def proxy_manager_for(self, *args, **kwargs):
                        context = create_urllib3_context(ciphers=CIPHERS)
                        kwargs['ssl_context'] = context
                        return super(DESAdapter, self).proxy_manager_for(*args, **kwargs)

                s = requests.Session()
                s.mount('https://www.zjkill.com/', DESAdapter())
                r = s.get(url)
                r.encoding = 'utf8'
                print r.text

因为我用的Python2.7,python3的解决办法就不写,那个更简单,可以自行网上搜索。
附上参考链接Configuring TLS With Requests

--结束END--

本文标题: 关于EOF occurred in v

本文链接: https://lsjlt.com/news/193113.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作