欢迎光临
我们一直在努力

关于 python 的 http 客户端的默认请求头测试 —— requests、httpx、curl_cffi

开始测试

首先测试 requests

测试代码

import requests

# 发起 HTTP GET 请求
url = "http://xxx.xxx.xxx.xxx:8086"


response = requests.get(url)

# 打印结果
print("Response Content (bytes):", response.content)
print("Response Text (str):", response.text)

服务端收到的请求头

{
    "host": "xxx.xxx.xxx.xxx:8086",
    "user-agent": "python-requests/2.31.0",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br",
    "connection": "close"
}

测试 httpx

测试代码

import httpx


# 发起 HTTP GET 请求
url = "http://xxx.xxx.xxx.xxx:8086"


with httpx.Client() as client:
    response = client.get(url)

# 打印结果
print("Response Content (bytes):", response.content)
print("Response Text (str):", response.text)

服务端收到的请求头

{
    "host": "xxx.xxx.xxx.xxx:8086",
    "user-agent": "python-httpx/0.24.0",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br",
    "connection": "close"
}

最后测试 curl_cffi

测试代码

from curl_cffi import requests

# 发起 HTTP GET 请求
url = "http://xxx.xxx.xxx.xxx:8086"


response = requests.get(url)

# 打印结果
print("Response Content (bytes):", response.content)
print("Response Text (str):", response.text)

服务端收到的请求头

{
    "host": "xxx.xxx.xxx.xxx:8086",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br, zstd"
}

https://segmentfault.com/a/1190000045992708

未经允许不得转载:IT极限技术分享汇 » 关于 python 的 http 客户端的默认请求头测试 —— requests、httpx、curl_cffi

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址