调用web3.geth.admin.start_ws报错:AttributeError: module 'web3.geth' has no attribute 'admin'
解决AttributeError: module 'web3.geth' has no attribute 'admin'
先确保Geth节点启动时开启admin API
admin是Geth的私有API,启动节点时必须显式开启该接口,同时配置HTTP/WS服务。示例启动命令:geth --http --http.api eth,net,web3,admin --http.port 8545 --ws --ws.api eth,net,web3,admin --ws.port 8546核心是
--http.api和--ws.api参数里必须包含admin,否则web3无法识别该命名空间。验证web3实例连接的节点支持admin接口
先通过简单调用确认节点是否暴露admin API:from web3 import Web3 w3 = Web3(Web3.HTTPProvider('http://localhost:8545')) # 尝试调用admin下的基础方法,能正常返回则说明接口可用 print(w3.geth.admin.node_info())确认web3.py版本为v5
你参考的是v5版本文档,需确保本地安装对应版本:pip show web3版本不符则升级:
pip install web3==5.31.0 # 可替换为最新v5稳定版正确初始化web3实例后再调用admin方法
确保连接到本地开启了admin API的Geth节点,再执行你的代码:from web3 import Web3 w3 = Web3(Web3.HTTPProvider('http://localhost:8545')) w3.geth.admin.start_ws(host='localhost', port=8546, cors="", apis="eth, net, web3")
注意:公共节点(如Infura、Alchemy)不会开放admin这类私有API,必须使用本地自行运行的Geth节点。
内容的提问来源于stack exchange,提问作者Bemz
相关产品推荐
相关产品推荐

