当前位置:   article > 正文

【自动化测试】web3py 连接 goerli_from web3 import web3, httpprovi

from web3 import web3, httpprovi

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")))
  • 1
  • 2
  • 3
  • error info:
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 401
  • 1

换一种方式

from web3 import Web3, HTTPProvider
connection = Web3(HTTPProvider('https://goerli.infura.io/v3/<API-KEY>'))
print ("Latest Ethereum block number", connection.eth.blockNumber)
  • 1
  • 2
  • 3

但没有 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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

从报错看的需要一个 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55828
推荐阅读
相关标签
  

闽ICP备14008679号