requests.exceptions.SSLError解决方案

遇到requests.exceptions.SSLError: HTTPSConnectionPool(host='jxj.xuancheng.gov.cn', port=443): Max retries exceeded with url: /News/showList/2207/page_1.html (Caused by SSLError(SSLError(1, '[SSL: BAD_ECPOINT] bad ecpoint (_ssl.c:997)')))的解决办法

方法一:安装pip3 install curl_cffi,导入from curl_cffi import requests;其他跟requests请求一样

requests.exceptions.SSLError解决方案_第1张图片方法二:修改requests的Cipher Suits中的加密算法

事实上在requests里面,要修改Cipher Suits中的加密算法,需要修改urllib3里面的ssl上下文,并实现一个新的HTTP适配器(HTTPAdapter)。 debug跟踪到了几处可能可以修改TLS握手特征的代码 路径如下:/usr/local/lib/python3.9/site-packages/urllib3/util/ssl_.py其中,session.mount的第一个参数表示这个适配器只在https://**.cn开头的网址中生效。

# encoding: utf-8
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.ssl_ import create_urllib3_context

ciphers = ":".join(
    [
        "DH+AES",
        "RSA+AES",
    ]
)

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
}


class DESAdapter(HTTPAdapter):
    """
    更改适配器,修改加密算法
    """
    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 get_page(session):
    response = session.get(f'https:///681419**1?pageIndex=3', headers=headers, verify=False)
    print(response.text)

session = requests.Session()
session.mount('https://**.cn', DESAdapter())
get_page(session)

你可能感兴趣的:(linux,运维,服务器)