I am trying to listen for events emitted by a simple Solidity contract. I am not able to connect using ws though I can commit transactions using http.

Here's my code for event listening -

import time
import json
from web3 import Web3, HTTPProvider, WebsocketProvider
contract_address = '0x6891c4e1171Fb5ec0B77734E49c62077066FBB4D'
wallet_address = '0x8093d4d4C6eFea785eaeba5430Bb62574C113214'

w3 = Web3(WebsocketProvider('ws://'))
print("Connection established : ", w3.isConnected())

contractDetails = json.load(open("build/contracts/MyContract.json"))
contract = w3.eth.contract(address = contract_address, abi = contractDetails.get('abi'))

evt_filter = contract.events.BroadcastEvent.createFilter(fromBlock='latest') #-- latest for my event

while True :
evnts = evt_filter.get_new_entries()
if len(evnts) > 0:

Here the error I get when trying to check isConnected() -

Traceback (most recent call last):
File "./event_listener.py", line 16, in <module>
print("Connection established : ", w3.isConnected())
File "/home/rranjan/.local/lib/python3.6/site-packages/web3/main.py", line 258, in isConnected
return self.provider.isConnected()
File "/home/rranjan/.local/lib/python3.6/site-packages/web3/providers/base.py", line 102, in isConnected
response = self.make_request(RPCEndpoint('web3_clientVersion'), [])
File "/home/rranjan/.local/lib/python3.6/site-packages/web3/providers/websocket.py", line 137, in make_request
return future.result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in getresult
raise self._exception
File "/home/rranjan/.local/lib/python3.6/site-packages/web3/providers/websocket.py", line 125, in coro_make_request
File "/usr/lib/python3.6/asyncio/tasks.py", line 362, in wait_for
raise futures.TimeoutError()

This would happen when I am trying to call createFilter straightaway(not checking the connection) as well.

What could be wrong ? web3 version - 5.4.0 Python - 3.6.8

Your Answer

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

Browse other questions tagged or ask your own question.