python让requetst库支持https2.0及修改默认的tls
标签搜索
侧边栏壁纸
  • 累计撰写 25 篇文章
  • 累计收到 31 条评论

python让requetst库支持https2.0及修改默认的tls

z
z
2021-11-09 / 0 评论 / 13 阅读 / 正在检测是否收录...

除了用httpx模块,还可以魔改下requests库
来自大佬Artio的分享

import urllib3.util.ssl_
import requests
urllib3.util.ssl_.DEFAULT_CIPHERS = ":".join(
        i for i in urllib3.util.ssl_.DEFAULT_CIPHERS.split(":") if not i.startswith("!"))

from requests import Session
from hyper.contrib import HTTP20Adapter
from requests.utils import CaseInsensitiveDict

_request = Session.request


class MyHttp20Adapter(HTTP20Adapter):
    def __init__(self, *args, **kwargs):
        super(MyHttp20Adapter, self).__init__(*args, **kwargs)
        super(HTTP20Adapter, self).__init__(*args, **kwargs)


def request(self, method, url, clear_headers=False, http2=False, **kwargs):
    if http2:
        self.mount("https://", MyHttp20Adapter())

    if clear_headers:
        self.headers = CaseInsensitiveDict()
    result = _request(self, method=method, url=url, **kwargs)
    return result


r=requests.Session()
r.headers= {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
}
data = {
  'page': '2'
}
res=request(r,method="post",url="",http2=True,data=data)
print(res.text)
0

评论 (0)

取消