0

I am trying to run this code

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)

But i keep getting this error

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',))

I am new to all of this. Can someone please tell me what I am doing wrong? I am trying to run the code on repl.it

  • The URL that you are providing is invalud, are you running a local node? – Jaime Nov 7 '18 at 6:37
  • Thanks for your answer! No I am not. This is a sample code I found online. I am just trying to figure out how to run it. I am new the web3.py and ethereum as a whole. How do I run a local node? – user3078335 Nov 7 '18 at 7:33
1

You need to have access to a node. You can access infura nodes that are freely accesible:

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

You can get your personal key from infura that you can use instead of kjbuiysuidygasdy by registering for free in Infura

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.