使用dnspython查询域名A记录时遇httpcore AttributeError求助
解决
AttributeError: module 'httpcore' has no attribute 'CloseError'问题 问题描述
运行以下DNS查询代码时触发错误:
import dns import dns.resolver result = dns.resolver.query('tutorialspoint.com', 'A') for ipval in result: print('IP', ipval.to_text())
错误信息:
AttributeError: module 'httpcore' has no attribute 'CloseError'
已尝试重装dnspython(包名dns),问题未解决。
问题分析
该错误与dnspython本身无关,根源是httpcore包版本不兼容:
- 高版本的httpcore已移除
CloseError属性 - 或当前环境中存在依赖httpcore的其他包(如httpx),版本不匹配导致冲突
解决方案
方案1:调整httpcore到兼容版本
执行命令安装包含CloseError的稳定版本:
pip install httpcore==0.17.3
方案2:同步依赖包版本(若使用httpx)
如果你的环境中用到了httpx,需保证httpx与httpcore版本匹配,例如:
pip install httpx==0.24.1 httpcore==0.17.3
方案3:卸载无关依赖(若不需要httpcore/httpx)
你的DNS查询代码不需要httpcore包,可能是其他依赖自动安装的,直接卸载即可:
pip uninstall -y httpcore httpx
验证
完成上述操作后,重新运行你的代码,应该能正常输出目标域名的IP地址。
内容的提问来源于stack exchange,提问作者Dhrumil patel
相关产品推荐
相关产品推荐

