web3.py – Understanding and Managing Peers in web3.py

web3.py

I am trying to get the number of peers that my node is connected to.

I am connected to INFURA endpoint:

from web3 import Web3
w3 = Web3(
    Web3.HTTPProvider(("https://mainnet.infura.io/v3....."))
)

print(w3.geth.admin.peers()) // Gives error

I receive following error:

ValueError: {'code': -32601, 'message': 'The method admin_peers does not exist/is not available'}

Am I doing something wrong?

I made sure I am connected by:

>>> w3.isConnected()
True

Best Answer

Following line should solve your issue:

print(w3.net.peerCount)
Related Topic