Testrpc

執行 web3.py 程式碼的問題

  • November 7, 2018

我正在嘗試執行此程式碼

from web3 import Web3, HTTPProvider
import json
import pprint
import requests
from random import randint

# Debug flags
debug_transaction = False
debug_CALL_transactions = False

# Geth node parameters
rpcport = '4000'

# Experiment parameters
numBuckets = 50

# Instantiate web3
web3 = Web3(HTTPProvider('http://localhost:' + rpcport))
# Instantiate HTTP connection to Geth JSONRPC
session = requests.Session()

# Iterate over blocks and print transactions
latest = web3.eth.getBlock('latest').number

print (latest)

但我不斷收到這個錯誤

Traceback (most recent call last):
 File "python", line 23, in <module>
 File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
 File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
 File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
 File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
 File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=4000): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fcaeb556f98>: Failed to establish a new connection: [Errno 111] Connection refused',))

我對這一切都很陌生。有人可以告訴我我做錯了什麼嗎?我正在嘗試在 repl.it 上執行程式碼

您需要有權訪問節點。您可以訪問可自由訪問的 infura 節點:

web3 = Web3(HTTPProvider('https://mainnet.infura.io/kjbuiysuidygasdy'))
web3.eth.getBlock('latest').number

您可以從 infura 獲取您可以使用的個人密鑰,而不是通過在Infurakjbuiysuidygasdy免費註冊

希望這可以幫助。

引用自:https://ethereum.stackexchange.com/questions/61884