python-httpx的使用

HTTPX是Python3的功能齐全的HTTP客户端,它提供同步和异步API,并支持HTTP/1.1和HTTP/2

安装

pip install httpx

创建请求

通过httpx库发出一个请求非常简单,如下:

import httpx

response = httpx.get('https://www.baidu.com/')
print(type(response), response)     # <class 'httpx.Response'> <Response [200 OK]>

同样,我们再来发出一个POST请求:

response = httpx.post('http://localhost:5000/login', data={'username': 'httpx', 'password': '123456'})

PUT, DELETE, HEADOPTIONS请求都遵循相同的样式:

response = httpx.put('http://www.baidu.com/', data={key: value})
response = httpx.head('http://www.baidu.com/')
response = httpx.delete('http://www.baidu.com/')
response = httpx.options('http://www.baidu.com/')

自定义头部

要在传入请求中包含其他标头,请使用headers关键字参数:

header = {"user-agent": 'my_test/0001'}
response = httpx.get("https://api.github.com/events", headers=header)

超时时间

httpx设置默认的超时时间为5秒,超过此时间未响应将引发错误。我们可以通过timeout关键字参数来手动修改它:

response = httpx.get('http://localhost:5000/update', timeout=10)

你也可以将其设置为None完全禁用超时行为

response = httpx.get('http://localhost:5000/update', timeout=None)

超时又可以分为connectreadwritepool超时。如果想详细设置,我们可以通过httpx.Timeout类来实现:

# 读取超时为10s,其他超时为5秒
timeout = httpx.Timeout(5, read=10)
response = httpx.get('http://localhost:5000/update', timeout=timeout)

SSL证书

通过httpx发出HTTPS请求时,需要验证所请求主机的身份。我们可以通过verify来指定我们存在的CA证书:

response = httpx.get('https://example.org', verify='../../client.pem')

或者你可以传递标准库ssl.SSLContext

import ssl
import httpx

context = ssl.create_default_context()
context.load_verify_locations(cafile='../../client.pem')
response = httpx.get('https://example.org', verify='../../client.pem')

又或者,你可以将verify设置为False禁用SSL验证:

response = httpx.get('https://example.org', verify=False)

认证

HTTPX支持Basic AuthDigest Auth身份验证。要提供身份验证凭据,请将2个元组得纯文本strbytes对象作为auth参数传递给请求函数:

response
  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要安装httpx模块,你可以使用pip命令。在命令行中输入以下命令即可安装httpx模块: ``` pip install httpx ``` 安装完成后,你就可以在Python代码中导入httpx模块并使用它了。例如,你可以使用以下代码导入httpx模块并发送GET请求: ```python import httpx url = 'https://spa16.scrape.center/' 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 Edg/96.0.1054.62', 'referer': 'https://scrape.center/' } client = httpx.Client(http2=True) response = client.get(url, headers=headers) print(response.text) print(response.http_version) ``` 这段代码使用httpx发送了一个GET请求,并打印了响应的内容和使用的传输协议版本。\[1\]希望这可以帮助到你。 #### 引用[.reference_title] - *1* [安装httpx模块](https://blog.csdn.net/m0_61017055/article/details/122270143)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Python httpx模块教程](https://blog.csdn.net/haloged/article/details/130452294)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值