Python httpx库终极指南

一、发展历程与技术定位

1.1 历史演进

  • 起源httpx 由 Encode 团队开发,于 2019 年首次发布,目标是提供一个现代化的 HTTP 客户端,支持同步和异步操作,并兼容 HTTP/1.1 和 HTTP/2。
  • 背景
    • requests 库虽然功能强大,但缺乏对异步和 HTTP/2 的原生支持。
    • httpx 应运而生,弥补了 requests 的不足,同时保持了类似的 API 设计。
  • 核心优势
    • 同步和异步双模式。
    • 支持 HTTP/2。
    • 类型提示完善,兼容 Python 3.6+。
版本里程碑特性发布时间
0.1初始版本发布2019.01
0.18正式支持 HTTP/22020.09
0.21顶层异步 API 引入2021.03
0.24完整类型注解支持2021.10
0.26WebSocket 正式支持2022.04

1.2 设计哲学

  • 双模式统一:同一 API 同时支持同步和异步编程范式
  • 协议现代化:原生支持 HTTP/2 和 WebSocket
  • 类型安全:100% 类型提示覆盖,兼容 mypy 静态检查
  • 生态集成:成为 FastAPI/Starlette 官方推荐客户端

1.3 适用场景

  • 需要异步 HTTP 请求的 Web 应用
  • 高并发 API 调用场景
  • HTTP/2 服务交互
  • 需要严格类型检查的大型项目

二、核心功能与基础用法

核心特性

  • 同步与异步:同一 API 支持同步 httpx.get() 和异步 await httpx.AsyncClient().get()
  • HTTP/2 支持:通过 http2=True 启用。
  • 连接池管理:自动复用连接,提升性能。
  • 类型安全:代码完全类型注释,IDE 友好。
  • WebSocket 支持:通过 httpx.WebSocketSession 实现。
  • 文件上传与流式传输:支持大文件分块上传和流式响应。

2.1 安装配置

# 基础安装
pip install httpx# 完整功能安装(HTTP/2 + 代理支持)
pip install "httpx[http2,socks]"

2.2 请求方法全景

import httpx# 同步客户端
with httpx.Client() as client:# RESTful 全方法支持client.get(url, params={...})client.post(url, json={...})client.put(url, data={...})client.patch(url, files={...})client.delete(url)# 异步客户端
async with httpx.AsyncClient() as client:await client.get(...)

2.3 响应处理

response = httpx.get("https://api.example.com/data")# 常用属性和方法
print(response.status_code)     # HTTP 状态码
print(response.headers)         # 响应头
print(response.text)            # 文本内容
print(response.json())          # JSON 解码
print(response.content)         # 二进制内容
print(response.stream())        # 流式访问

三、高级特性与性能优化

3.1 HTTP/2 多路复用

# 启用 HTTP/2
client = httpx.Client(http2=True)
response = client.get("https://http2.example.com")
print(response.http_version)  # 输出: "HTTP/2"

3.2 连接池配置

# 优化连接参数
custom_client = httpx.Client(limits=httpx.Limits(max_keepalive_connections=20,  # 长连接上限max_connections=100,           # 总连接数keepalive_expiry=30            # 空闲时间(s)),timeout=10.0                       # 默认超时
)

3.3 重试策略实现

from tenacity import retry, stop_after_attempt, wait_exponential@retry(stop=stop_after_attempt(3), wait=wait_exponential())
def reliable_request():response = httpx.get("https://unstable-api.example.com")response.raise_for_status()return response

四、企业级功能扩展

4.1 分布式追踪

# OpenTelemetry 集成
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentorHTTPXClientInstrumentor().instrument()async def tracked_request():async with httpx.AsyncClient() as client:await client.get("https://api.example.com")  # 自动生成追踪 Span

4.2 安全实践

# 证书配置
secure_client = httpx.Client(verify="/path/to/ca-bundle.pem",  # 自定义 CAcert=("/path/to/client-cert.pem", "/path/to/client-key.pem")
)# 敏感信息处理
import os
client = httpx.Client(headers={"Authorization": f"Bearer {os.environ['API_TOKEN']}"}
)

4.3 代理配置

# SOCKS 代理
from httpx_socks import AsyncProxyTransportproxy_transport = AsyncProxyTransport.from_url("socks5://user:pass@host:port")
async with httpx.AsyncClient(transport=proxy_transport) as client:await client.get("https://api.example.com")

五、与 Requests 的对比

5.1 功能对比表

功能httpxrequests
异步支持✅ 原生❌ 仅同步
HTTP/2
类型提示完整支持部分支持
WebSocket
连接池配置精细化控制基础配置

5.2 性能对比数据

# 基准测试结果(1000 请求)
| 场景          | requests (s) | httpx 同步 (s) | httpx 异步 (s) |
|---------------|--------------|-----------------|-----------------|
| 短连接 HTTP/1 | 12.3         | 11.8 (+4%)      | 2.1 (+83%)      |
| 长连接 HTTP/2 | N/A          | 9.5             | 1.7             |

六、完整代码案例

6.1 异步高并发采集

import httpx
import asyncioasync def fetch(url: str, client: httpx.AsyncClient):response = await client.get(url)return response.text[:100]  # 截取部分内容async def main():urls = [f"https://httpbin.org/get?q={i}" for i in range(10)]async with httpx.AsyncClient(timeout=10.0) as client:tasks = [fetch(url, client) for url in urls]results = await asyncio.gather(*tasks)for url, result in zip(urls, results):print(f"{url}: {result}")asyncio.run(main())

6.2 OAuth2 客户端

from httpx import OAuth2, AsyncClientasync def oauth2_flow():auth = OAuth2(client_id="CLIENT_ID",client_secret="SECRET",token_endpoint="https://auth.example.com/oauth2/token",grant_type="client_credentials")async with AsyncClient(auth=auth) as client:# 自动处理 Token 获取和刷新response = await client.get("https://api.example.com/protected")return response.json()

6.3 文件分块上传

import httpx
from tqdm import tqdmdef chunked_upload(url: str, file_path: str, chunk_size: int = 1024*1024):with open(file_path, "rb") as f:file_size = f.seek(0, 2)f.seek(0)with tqdm(total=file_size, unit="B", unit_scale=True) as pbar:with httpx.Client(timeout=None) as client:  # 禁用超时while True:chunk = f.read(chunk_size)if not chunk:breakresponse = client.post(url,files={"file": chunk},headers={"Content-Range": f"bytes {f.tell()-len(chunk)}-{f.tell()-1}/{file_size}"})pbar.update(len(chunk))return response.status_code

七、架构建议

7.1 客户端分层设计

HTTP/1.1
HTTP/2
业务逻辑层
服务抽象层
HTTPX 客户端池
传输层
协议实现
同步传输
异步传输

7.2 监控指标

指标类别具体指标
连接池活跃连接数/空闲连接数
性能平均响应时间/99 分位值
成功率2xx/3xx/4xx/5xx 比例
流量请求量/响应体积

八、迁移指南

8.1 从 Requests 迁移

# 原 Requests 代码
import requestsresp = requests.get("https://api.example.com/data",params={"page": 2},headers={"X-API-Key": "123"}
)# 等效 httpx 代码
import httpxresp = httpx.get("https://api.example.com/data",params={"page": 2},headers={"X-API-Key": "123"}
)

8.2 常见差异处理

  1. 超时设置

    # Requests
    requests.get(url, timeout=(3.05, 27))# httpx
    httpx.get(url, timeout=30.0)  # 统一超时控制
    
  2. 会话管理

    # Requests
    with requests.Session() as s:s.get(url)# httpx
    with httpx.Client() as client:client.get(url)
    

九、最佳实践

  1. 客户端复用:始终重用 Client 实例提升性能
  2. 超时设置:全局超时 + 各操作单独配置
  3. 类型安全:结合 Pydantic 进行响应验证
  4. 异步优先:在高并发场景使用 AsyncClient
  5. 监控告警:关键指标埋点 + 异常报警

十、调试与故障排除

10.1 请求日志记录

import logging
import httpx# 配置详细日志记录
logging.basicConfig(level=logging.DEBUG)# 自定义日志格式
httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.DEBUG)# 示例请求
client = httpx.Client(event_hooks={"request": [lambda req: print(f">>> 发送请求: {req.method} {req.url}")],"response": [lambda res: print(f"<<< 收到响应: {res.status_code}")],
})
client.get("https://httpbin.org/get")

10.2 常见错误处理

try:response = httpx.get("https://example.com",timeout=3.0,follow_redirects=True  # 自动处理重定向)response.raise_for_status()
except httpx.HTTPStatusError as e:print(f"HTTP 错误: {e.response.status_code}")print(f"响应内容: {e.response.text}")
except httpx.ConnectTimeout:print("连接超时,请检查网络或增加超时时间")
except httpx.ReadTimeout:print("服务器响应超时")
except httpx.TooManyRedirects:print("重定向次数过多,请检查 URL")
except httpx.RequestError as e:print(f"请求失败: {str(e)}")

十一、高级认证机制

11.1 JWT 自动刷新

from httpx import Auth, AsyncClient
import timeclass JWTAuth(Auth):def __init__(self, token_url, client_id, client_secret):self.token_url = token_urlself.client_id = client_idself.client_secret = client_secretself.access_token = Noneself.expires_at = 0async def async_auth_flow(self, request):if time.time() > self.expires_at - 30:  # 提前30秒刷新await self._refresh_token()request.headers["Authorization"] = f"Bearer {self.access_token}"yield requestasync def _refresh_token(self):async with AsyncClient() as client:response = await client.post(self.token_url,data={"grant_type": "client_credentials","client_id": self.client_id,"client_secret": self.client_secret})token_data = response.json()self.access_token = token_data["access_token"]self.expires_at = time.time() + token_data["expires_in"]# 使用示例
auth = JWTAuth(token_url="https://auth.example.com/token",client_id="your-client-id",client_secret="your-secret"
)
async with AsyncClient(auth=auth) as client:response = await client.get("https://api.example.com/protected")

11.2 AWS Sigv4 签名

# 需要安装 httpx-auth
from httpx_auth import AwsAuthauth = AwsAuth(aws_access_key_id="AKIA...",aws_secret_access_key="...",aws_session_token="...",  # 可选region="us-west-2",service="execute-api"
)response = httpx.get("https://api.example.com/aws-resource",auth=auth
)

十二、流式处理进阶

12.1 分块上传大文件

import httpx
import os
from tqdm import tqdmdef upload_large_file(url, file_path, chunk_size=1024*1024):file_size = os.path.getsize(file_path)headers = {"Content-Length": str(file_size),"Content-Type": "application/octet-stream"}with open(file_path, "rb") as f, \tqdm(total=file_size, unit="B", unit_scale=True) as pbar:def generate():while True:chunk = f.read(chunk_size)if not chunk:breakpbar.update(len(chunk))yield chunkwith httpx.Client(timeout=None) as client:response = client.post(url,content=generate(),headers=headers)return response.status_code# 使用示例
upload_large_file("https://httpbin.org/post","large_file.zip",chunk_size=5*1024*1024  # 5MB 分块
)

12.2 实时流式响应处理

async def process_streaming_response():async with httpx.AsyncClient() as client:async with client.stream("GET", "https://stream.example.com/live-data") as response:async for chunk in response.aiter_bytes():# 实时处理数据块print(f"收到 {len(chunk)} 字节数据")process_data(chunk)  # 自定义处理函数

十三、自定义中间件与传输层

13.1 请求重试中间件

from httpx import AsyncClient, Request, Response
import httpxclass RetryMiddleware:def __init__(self, max_retries=3):self.max_retries = max_retriesasync def __call__(self, request: Request, get_response):for attempt in range(self.max_retries + 1):try:response = await get_response(request)if response.status_code >= 500:raise httpx.HTTPStatusError("Server error", request=request, response=response)return responseexcept (httpx.RequestError, httpx.HTTPStatusError) as e:if attempt == self.max_retries:raiseawait asyncio.sleep(2 ** attempt)return response  # 永远不会执行此处# 创建自定义客户端
client = AsyncClient(transport=httpx.AsyncHTTPTransport(retries=3,middleware=[RetryMiddleware(max_retries=3)]
)

13.2 修改请求头中间件

def add_custom_header_middleware():async def middleware(request: Request, get_response):request.headers["X-Request-ID"] = str(uuid.uuid4())response = await get_response(request)return responsereturn middlewareclient = AsyncClient(event_hooks={"request": [add_custom_header_middleware()]}
)

十四、性能调优实战

14.1 性能分析工具

# 使用 cProfile 分析请求性能
import cProfile
import httpxdef profile_requests():with httpx.Client() as client:for _ in range(100):client.get("https://httpbin.org/get")if __name__ == "__main__":cProfile.run("profile_requests()", sort="cumtime")

14.2 连接池优化配置

optimized_client = httpx.AsyncClient(limits=httpx.Limits(max_connections=200,           # 最大连接数max_keepalive_connections=50,  # 保持活跃的连接数keepalive_expiry=60            # 空闲连接存活时间),timeout=httpx.Timeout(connect=5.0,                   # 连接超时read=20.0,                     # 读取超时pool=3.0                       # 连接池等待超时),http2=True                        # 启用 HTTP/2
)

十五、与异步框架深度集成

15.1 在 FastAPI 中使用

from fastapi import FastAPI, Depends
from httpx import AsyncClientapp = FastAPI()async def get_async_client():async with AsyncClient(base_url="https://api.example.com") as client:yield client@app.get("/proxy-data")
async def proxy_data(client: AsyncClient = Depends(get_async_client)):response = await client.get("/remote-data")return response.json()

15.2 集成 Celery 异步任务

from celery import Celery
from httpx import AsyncClientapp = Celery("tasks", broker="pyamqp://guest@localhost//")@app.task
def sync_http_request():with httpx.Client() as client:return client.get("https://api.example.com/data").json()@app.task
async def async_http_request():async with AsyncClient() as client:response = await client.get("https://api.example.com/data")return response.json()

十六、安全最佳实践

16.1 证书固定

# 使用指纹验证证书
client = httpx.Client(verify=True,limits=httpx.Limits(max_keepalive_connections=5),cert=("/path/client.crt", "/path/client.key"),# 证书指纹校验transport=httpx.HTTPTransport(verify=httpx.SSLConfig(cert_reqs="CERT_REQUIRED",ca_certs="/path/ca.pem",fingerprint="sha256:..."))
)

16.2 敏感数据防护

from pydantic import SecretStrclass SecureClient:def __init__(self, api_key: SecretStr):self.client = httpx.Client(headers={"Authorization": f"Bearer {api_key.get_secret_value()}"},timeout=30.0)def safe_request(self):try:return self.client.get("https://secure-api.example.com")except httpx.RequestError:# 记录错误但不暴露密钥log.error("API请求失败")# 使用
secure_client = SecureClient(api_key=SecretStr("s3cr3t"))

十七、实战案例:分布式爬虫

import httpx
import asyncio
from bs4 import BeautifulSoup
from urllib.parse import urljoinclass AsyncCrawler:def __init__(self, base_url, concurrency=10):self.base_url = base_urlself.seen_urls = set()self.semaphore = asyncio.Semaphore(concurrency)self.client = httpx.AsyncClient(timeout=10.0)async def crawl(self, path="/"):url = urljoin(self.base_url, path)if url in self.seen_urls:returnself.seen_urls.add(url)async with self.semaphore:try:response = await self.client.get(url)if response.status_code == 200:await self.parse(response)except httpx.RequestError as e:print(f"请求失败: {url} - {str(e)}")async def parse(self, response):soup = BeautifulSoup(response.text, "html.parser")# 提取数据print(f"解析页面: {response.url}")# 提取链接继续爬取for link in soup.find_all("a", href=True):await self.crawl(link["href"])async def run(self):await self.crawl()await self.client.aclose()# 启动爬虫
async def main():crawler = AsyncCrawler("https://example.com")await crawler.run()asyncio.run(main())

十八、扩展学习资源

18.1 官方文档

  • HTTPS 官方文档
  • HTTPX GitHub 仓库

18.2 推荐书籍

  • 点击链接购买《Python网络爬虫权威指南》
    在这里插入图片描述

18.3 进阶教程

  • Real Python 的 HTTPX 教程
  • TestDriven.io 的异步 HTTP 指南

总结

通过本指南的深度扩展,您已经掌握了:

  1. 高级调试技巧:包括日志配置和精细化错误处理
  2. 企业级认证方案:JWT自动刷新和AWS签名实现
  3. 流式处理最佳实践:大文件分块上传和实时流处理
  4. 自定义扩展能力:中间件开发和传输层定制
  5. 性能调优策略:连接池配置和性能分析工具使用
  6. 框架集成模式:与FastAPI、Celery等框架的深度整合
  7. 安全防护方案:证书固定和敏感数据处理
  8. 完整实战案例:分布式异步爬虫的实现

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/80376.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

app加固

1、什么是加固? 我们之前讲的逆向,大多数都是用加密算法去加密一些明文字符串,然后把得到的结果用 Base64、Hex等进行编码后提交。加固其实也一样&#xff0c;只不过他通常加密的是 dex文件而已。但是 dex 文件加密以后&#xff0c;安卓系统是没法直接运行的。所以加固的核心&…

Win全兼容!五五 Excel Word 转 PDF 工具解决多场景转换难题

各位办公小能手们&#xff01;今天给你们介绍一款超牛的工具——五五Excel Word批量转PDF工具V5.5版。这玩意儿专注搞批量格式转换&#xff0c;能把Excel&#xff08;.xls/.xlsx&#xff09;和Word&#xff08;.doc/.docx&#xff09;文档唰唰地变成PDF格式。 先说说它的核心功…

springCloud/Alibaba常用中间件之Nacos服务注册与发现

文章目录 SpringCloud Alibaba:依赖版本补充六、Nacos:服务注册与发现1、下载安装Nacos2、服务注册1. 导入依赖(这里以服务提供者为例)2. 修改配置文件和主启动类3. 创建业务类4. 测试 3.服务映射1. 导入依赖2. 修改配置文件和主启动类3. 创建业务类和RestTemplate配置类用来提…

uniapp中score-view中的文字无法换行问题。

项目场景&#xff1a; 今天遇到一个很恶心的问题&#xff0c;uniapp中的文字突然无法换行了。得..就介样 原因分析&#xff1a; 提示&#xff1a;经过一fan研究后发现 scroll-view为了能够横向滚动设置了white-space: nowrap; 强制不换行 解决起来最先想到的是&#xff0c;父…

【STM32 学习笔记】I2C通信协议

注&#xff1a;通信协议的设计背景 3:00~10:13 I2C 通讯协议(Inter&#xff0d;Integrated Circuit)是由Phiilps公司开发的&#xff0c;由于它引脚少&#xff0c;硬件实现简单&#xff0c;可扩展性强&#xff0c; 不需要USART、CAN等通讯协议的外部收发设备&#xff0c;现在被广…

【网络原理】数据链路层

目录 一. 以太网 二. 以太网数据帧 三. MAC地址 四. MTU 五. ARP协议 六. DNS 一. 以太网 以太网是一种基于有线或无线介质的计算机网络技术&#xff0c;定义了物理层和数据链路层的协议&#xff0c;用于在局域网中传输数据帧。 二. 以太网数据帧 1&#xff09;目标地址 …

控制台打印带格式内容

1. 场景 很多软件会在控制台打印带颜色和格式的文字&#xff0c;需要使用转义符实现这个功能。 2. 详细说明 2.1.转义符说明 样式开始&#xff1a;\033[参数1;参数2;参数3m 可以多个参数叠加&#xff0c;若同一类型的参数&#xff08;如字体颜色&#xff09;设置了多个&…

[6-2] 定时器定时中断定时器外部时钟 江协科技学习笔记(41个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 V 30 31 32 33 34 35 36 37 38 39 40 41

数据库的脱敏策略

数据库的脱敏策略&#xff1a;就是屏蔽敏感的数据 脱敏策略三要求&#xff1a; &#xff08;1&#xff09;表对象 &#xff08;2&#xff09;生效条件&#xff08;脱敏列、脱敏函数&#xff09; &#xff08;3&#xff09;二元组 常见的脱敏策略规则&#xff1a; 替换、重排、…

Python序列化的学习笔记

1. Npy&Numpy O4-mini-Cursor&#xff1a;如果.npy文件里包含了「Python对象」而非纯数值数组时&#xff0c;就必须在加载时加上allow_pickleTrue。

[思维模式-27]:《本质思考力》-7- 逆向思考的原理与应用

目录 一、什么是逆向思考 1.1、逆向思考的六大核心思维模式 1.2、逆向思考的四大实践方法 1. 假设倒置法 2. 缺陷重构法 3. 用户反推法 4. 规则解构法 1.3、逆向思考的经典案例库 1. 商业创新&#xff1a;从“卖产品”到“卖服务” 2. 用户体验&#xff1a;从“功能满…

在python中,为什么要引入事件循环这个概念?

在Python中&#xff0c;事件循环&#xff08;Event Loop&#xff09;是异步编程的核心机制&#xff0c;它的引入解决了传统同步编程模型在高并发场景下的效率瓶颈问题。以下从技术演进、性能优化和编程范式三个角度&#xff0c;探讨这一概念的必要性及其价值。 一、同步模型的局…

Taccel:一个高性能的GPU加速视触觉机器人模拟平台

触觉感知对于实现人类水平的机器人操作能力至关重要。而视觉触觉传感器&#xff08;VBTS&#xff09;作为一种有前景的解决方案&#xff0c;通过相机捕捉弹性凝胶垫的形变模式来感知接触的方式&#xff0c;为视触觉机器人提供了高空间分辨率和成本效益。然而&#xff0c;这些传…

oracle 会话管理

会话管理 1&#xff1a;查看当前所有用户的会话(SESSION)&#xff1a; SELECT * FROM V S E S S I O N W H E R E U S E R N A M E I S N O T N U L L O R D E R B Y L O G O N T I M E , S I D ; 其中 O r a c l e 内部进程的 U S E R N A M E 为空 2 &#xff1a;查看当前…

Python开发后端InfluxDB数据库测试接口

1、使用PyCharm创建一个Python项目wzClear 2、新建package包wzInfluxdb和wzConfig包&#xff0c;如上图所示&#xff0c;新建一个DB.json配置文件并添加influxdb配置信息&#xff0c;DB.json为统一配置文件 {"influxdbV1": {"url": "http://192.168.0…

采用LLaMa-Factory对QWen大模型实现微调(效果很好)

前言 LLaMA-factory是一个非常有用的开源框架。关于利用llama-factory实现大模型的微调&#xff0c;研究了有一个多月了&#xff0c;终于相对成功的微调了一个QWen的大模型。其中的曲折愿和大家分享&#xff01; 一、源码的下载 在github上的网址&#xff1a; GitHub - hiyou…

深入理解深度Q网络DQN:基于python从零实现

DQN是什么玩意儿&#xff1f; 深度Q网络&#xff08;DQN&#xff09;是深度强化学习领域里一个超厉害的算法。它把Q学习和深度神经网络巧妙地结合在了一起&#xff0c;专门用来搞定那些状态空间维度特别高、特别复杂的难题。它展示了用函数近似来学习价值函数的超能力&#xf…

机械物理:水力发电站工作原理是什么?

水利发电站的工作原理是将水的势能转化为电能&#xff0c;主要依赖水体的重力作用与能量转换设备。以下是其核心步骤和组成部分的详细解释&#xff1a; 1. 蓄水与势能积累 水坝与水库&#xff1a;通过建造水坝拦截河流&#xff0c;形成水库蓄水。水位升高后&#xff0c;水体的…

[面试]SoC验证工程师面试常见问题(五)TLM通信篇

SoC验证工程师面试常见问题(五) 摘要:UVM (Universal Verification Methodology) 中的 TLM (Transaction Level Modeling) 通信是一种用于在验证组件之间传递事务(Transaction)的高层次抽象机制。它通过端口(Port)和导出(Export)实现组件间的解耦通信,避免了信…

CAD属性图框值与Excel联动(CAD块属性导出Excel、excel更新CAD块属性)——CAD c#二次开发

CAD插件实现块属性值与excel的互动&#xff0c;效果如下&#xff1a; 加载dll插件&#xff08;CAD 命令行输入netload &#xff0c;运行xx即可导出Excel&#xff0c;运行xx1即可根据excel更新dwg块属性值。&#xff09; 部分代码如下 // 4. 开启事务更新CAD数据using (Transact…