You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 13:55:28