Files
pallectrum/electrum/scripts/block_headers.py

39 lines
1.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2013-10-06 12:28:45 +02:00
# A simple script that connects to a server and displays block headers
2014-07-29 10:43:04 +02:00
import time
2018-11-02 20:14:59 +01:00
import asyncio
2018-09-17 14:44:01 +02:00
2018-11-02 20:14:59 +01:00
from electrum.network import Network
from electrum.util import print_msg, json_encode, create_and_start_event_loop, log_exceptions
from electrum.simple_config import SimpleConfig
config = SimpleConfig()
2013-10-06 12:28:45 +02:00
# start network
2018-11-02 20:14:59 +01:00
loop, stopping_fut, loop_thread = create_and_start_event_loop()
network = Network(config)
network.start()
# wait until connected
2018-11-02 20:14:59 +01:00
while not network.is_connected():
time.sleep(1)
print_msg("waiting for network to get connected...")
2018-11-02 20:14:59 +01:00
header_queue = asyncio.Queue()
2013-10-06 12:28:45 +02:00
2018-11-02 20:14:59 +01:00
@log_exceptions
async def f():
try:
await network.interface.session.subscribe('blockchain.headers.subscribe', [], header_queue)
# 3. wait for results
while network.is_connected():
header = await header_queue.get()
print_msg(json_encode(header))
finally:
stopping_fut.set_result(1)
2013-10-06 12:28:45 +02:00
2018-11-02 20:14:59 +01:00
# 2. send the subscription
asyncio.run_coroutine_threadsafe(f(), loop)