【自动化测试】web3py 连接 goerli

发布时间:2023年12月17日

web3py 连接 goerli

直接使用库里方法

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:
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 401

换一种方式

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 的话

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

在这里插入图片描述
问题解决

资料

  • https://app.infura.io/dashboard/ethereum/748e605442564fd098d63afb85015a8f/settings/security
  • https://ethereum.stackexchange.com/questions/79215/project-id-is-required
文章来源:https://blog.csdn.net/u013948858/article/details/135024477
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。