每日一模块:httpx解决http2

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:Cloud
# datetime:2023/12/18
import httpx

"""
pip install httpx[http2] -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
"""
timeout = httpx.Timeout(2,
                        # 与服务器套接字连接最长时间ConnectTimeou
                        connect=0.5,
                        # 接收服务器数据块最长持续时间ReadTimeout
                        read=3,
                        # 发送数据块给服务器最长持续时间WriteTimeout
                        write=3)
http_client = httpx.Client(http2=True, timeout=timeout, verify=False)
url = "https://ip/goods"
request_data = dict(id="1111")

response_data = http_client.get(url=url,
                                headers=dict(FromCode="P205010101"),
                                params=request_data)
print(response_data.text, type(response_data.text))
print(response_data.json(), type(response_data.json()))
print(response_data.content)
print(response_data.status_code)
print(response_data.headers)

posted @ 2023-12-27 16:58  Alive_2020  阅读(101)  评论(0编辑  收藏  举报