Python发送digest认证的请求:requests.auth.HTTPDigestAuth/httpx.DigestAuth

ops/2024/9/25 5:25:28/

近日在做摄像头接口的调试,需要用到Digest认证,经过试验,代码如下:

一、同步版(pip install requests)

python">import requests
from requests.auth import HTTPDigestAuthhost = 'https://192.168.0.2'
path = '/api/xxx'
path2 = '/another/api'AUTH = ('username', 'password')
r = requests.get(host+path, verify=False, auth=HTTPDigestAuth(*AUTH))
print(r.status_code)
print(r.text)payload = {'a': 1}
r2 = requests.put(host+path2, verify=False, auth=HTTPDigestAuth(*AUTH), json=payload)
print(r2.status_code)
print(r2.json())

二、异步协程版(pip install httpx)

python">import asyncio
from httpx import AsyncClient, DigestAuthhost = 'https://192.168.0.2'
path = '/api/xxx'
path2 = '/another/api'AUTH = ('username', 'password')async def main():async with AsyncClient(base_url=host, verify=False, auth=DigestAuth(*AUTH)):r = await client.get(path)payload = {'a': 1}r2 = await client.put(path2, json=payload)print(r.status_code)print(r.text)print(r2.status_code)print(r2.json())if __name__ == '__main__':asyncio.run(main())

封装成工具类之后的代码如下

python">import osimport requests
from httpx import AsyncClient, DigestAuthAUTH = (os.environ["API_USER"], os.environ["API_PASS"])class HttpClient:PATH_GET = "/get-sth"PATH_PUT = "/put"@staticmethoddef sync_request(method: str, host: str, path: str, **kw) -> requests.Response:url = host.rstrip("/") + pathreturn requests.request(method, url, verify=False, auth=requests.auth.HTTPDigestAuth(*AUTH), **kw)@classmethoddef sync_get(cls, host: str, params=None, path: str | None = None) -> dict:if path is None:path = cls.PATH_GETr = cls.sync_request("GET", host, path, params=params)r.raise_for_status()return r.json()@classmethoddef sync_put(cls, payload: dict, host: str, path: str | None = None) -> dict:if path is None:path = cls.PATH_PUTr = cls.sync_request("PUT", host, path, json=payload)r.raise_for_status()return r.json()@staticmethoddef async_client(host: str) -> AsyncClient:return AsyncClient(base_url=host, verify=False, auth=DigestAuth(*AUTH))@classmethodasync def get(cls, host: str, params=None, path: str | None = None) -> dict:async with cls.async_client(host) as client:r = await client.get(path or cls.PATH_GET, params=params)return r.json()@classmethodasync def put(cls, payload: dict, host: str, path: str | None = None) -> dict:async with cls.async_client(host) as client:r = await client.put(path or cls.PATH_PUT, json=payload)return r.json()


http://www.ppmy.cn/ops/25162.html

相关文章

Git 核心概念与实操

这里写目录标题 1 版本回退2 工作区、暂存区、本地仓库、远程仓库3 分支合并3.1 Fast-forward3.2 Recursive3.3 Ours & Theirs 4 处理冲突5 git stash 存储工作区 参考:https://www.liaoxuefeng.com/wiki/896043488029600 1 版本回退 原文链接:http…

C++ 核心编程(2)

4.6.8 菱形继承 #include<iostream> #include <bits/stdc.h> using namespace std; //菱形继承 //动物类 class Animal{ public:int mAge; }; //羊 class Sheep : public Animal{}; //驼 class Tuo : public Animal{}; //羊驼 class SheepTuo:public Sheep,pu…

Json-server 模拟后端接口

json-server&#xff0c;模拟rest接口&#xff0c;自动生成增删改查接口。(官网地址&#xff1a;json-server - npm) 使用方法&#xff1a; 1. 安装json-server&#xff0c;npm i json-server -g 2. 创建json文件&#xff0c;文件中存储list数据&#xff0c;db.json {"…

实施运维工程师面试题

实施工程师面试题 (一)电脑网络,软硬件以及软件实施工程师要掌握的基本常识 两台电脑都在同一个网络环境中,A电脑访问不到B电脑的共享文件。此现象可能是哪些方面所至?如何处理?首先你要确定是不是在一个工作组内,只有在一个工作组内才可以共享文件,查看共享服务是否被…

stm32 hid自定义接收发送程序开发过程记录

cubleMX配置如下 修改端点描述符一次传输的数据大小 根据cubelMX标准在这里修改 编译错误 直接修改&#xff08;因为没有使用nodef &#xff09;编译通过 修改报告描述符&#xff08;默认的描述符无法传输数据&#xff09; 参考&#xff1a;USB协议详解第10讲&#xff08;USB描…

创维汽车亮相2024北京车展 100kW直流放电技术颠覆传统补能体系

在新质生产力的推动下&#xff0c;汽车行业正面临重塑产业格局、实现转型升级的迫切需求。4月25日&#xff0c;以“新时代 新汽车”为主题的2024北京国际汽车展览会拉开帷幕。作为拥有深厚制造业基因的企业&#xff0c;创维汽车于当日下午举办主题为“颠覆-开启移动补能新时代”…

电机介绍c

文章目录 1233.13.2 3.33.43.5 4 1 2 驱动用 赛车电源 控制用 打印机 停在那个位置 3 舵机 比较小&#xff1f; 3.1 DC就是直流 BDC操控方便 接个5号电池 正负极一反 电机就反&#xff08;电压控制 电压越大 转速越高&#xff09;噪声大这里寿命是连续 工作的&#xff08;寿命…

去中心化自治组织(DAO)

文章目录 一、DAO (Decentralized Autonomous Organization) 去中心化自治组织 二、举例说明 1、例子1 2、例子2 总结 一、DAO (Decentralized Autonomous Organization) 去中心化自治组织 DAO是一种基于区块链平台上的组织结构&#xff0c;它通过智能合约来实现组织的…