【自动化测试】web3py 连接 goerli

web3py 连接 goerli

直接使用库里方法

python 复制代码
if __name__ == '__main__':
    from web3.auto.infura.goerli import w3
    w3.eth.get_balance(get_address_by_private_key(os.getenv("AAA_KEY")))
  • error info:
text 复制代码
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 401

换一种方式

python 复制代码
from web3 import Web3, HTTPProvider
connection = Web3(HTTPProvider('https://goerli.infura.io/v3/<API-KEY>'))
print ("Latest Ethereum block number", connection.eth.blockNumber)

但没有 api-key 的话

shell 复制代码
Traceback (most recent call last):
  File "/Users/apple/github/qadev-py-scripts/test-web3/spider_eth.py", line 26, in <module>
    from web3.auto.infura.goerli import w3
  File "/Users/apple/miniconda/envs/web3/lib/python3.7/site-packages/web3/auto/infura/__init__.py", line 13, in <module>
    _infura_url = build_infura_url(INFURA_MAINNET_DOMAIN)
  File "/Users/apple/miniconda/envs/web3/lib/python3.7/site-packages/web3/auto/infura/endpoints.py", line 55, in build_infura_url
    key = load_api_key()
  File "/Users/apple/miniconda/envs/web3/lib/python3.7/site-packages/web3/auto/infura/endpoints.py", line 35, in load_api_key
    "No Infura Project ID found. Please ensure "
web3.exceptions.InfuraKeyNotFound: No Infura Project ID found. Please ensure that the environment variable WEB3_INFURA_PROJECT_ID is set.

从报错看的需要一个 WEB3_INFURA_PROJECT_ID, 去申请一个 infura id

问题解决

资料

相关推荐
开开心心_Every8 小时前
Win10/Win11版本一键切换工具
linux·运维·服务器·edge·pdf·web3·共识算法
devmoon1 天前
Polkadot Hub 智能合约中的账户体系
web3·区块链·智能合约·polkadot
暴躁小师兄数据学院1 天前
【WEB3.0零基础转行笔记】编程语言篇-第一讲:Go语言基础及环节搭建
笔记·golang·web3·区块链
老蒋每日coding2 天前
Web3 开发入门:用 Ethers.js 玩转以太坊交易与合约
web3·区块链
暴躁小师兄数据学院2 天前
【WEB3.0零基础转行笔记】基础知识篇—第一讲:区块链基础
笔记·web3·区块链
voidmort3 天前
web3中的共识:PBFT、Tendermint 与 DAG 共识
web3·区块链
CertiK4 天前
CertiK登上达沃斯官网,Web3安全进入主流视野
安全·web3
voidmort4 天前
web3中的共识:以太坊共识
web3·区块链
voidmort5 天前
Web3 中的 DEX 流程详解:从原理到实现
web3·区块链
devmoon5 天前
如何使用 Web3.py 与 Polkadot Hub 进行交互
web3·区块链·智能合约·交互·web3.py·solidity·polkadot