-
Notifications
You must be signed in to change notification settings - Fork 65
Python Subscription Examples
thecodefactory edited this page Jun 15, 2018
·
3 revisions
Libbitcoin server currently allows subscriptions for service Heartbeats, Blocks, and Transactions. An example python listener client is shown below for each, with sample output.
This example heartbeat subscription client is located here.
import sys
import zmq
import struct
# Connect to the public heartbeat service.
url = 'tcp://mainnet.libbitcoin.net:9092'
if len(sys.argv) > 1:
url = sys.argv[1]
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(url)
socket.setsockopt(zmq.SUBSCRIBE, '')
while True:
# Collect the response in parts.
sequence = struct.unpack('<H', socket.recv())[0]
height = struct.unpack('<Q', socket.recv())[0]
print '[Seq:{}] Heartbeat received with height {}'.format(
sequence, height)
Sample subscription output:
[Seq:2519] Heartbeat received with height 527570
[Seq:2520] Heartbeat received with height 527570
This example block subscription client is located here.
#!python3
import sys
import zmq
import struct
from binascii import hexlify, unhexlify
# Connect to the public block service.
url = 'tcp://mainnet.libbitcoin.net:9093'
if len(sys.argv) > 1:
url = sys.argv[1]
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(url)
socket.setsockopt(zmq.SUBSCRIBE, '')
while True:
# Collect the response in parts.
sequence = struct.unpack('<H', socket.recv())[0]
height = struct.unpack('<I', socket.recv())[0]
block_data = hexlify(socket.recv()).decode('ascii')
print '[Seq:{}] Block {} received {}'.format(
sequence, height, block_data)
Sample output (truncated here for readability):
[Seq:6869] Block 527572 received 000000208f5005848e151debe992d81800242acb4774846094083600000000000000000087b3cb54f273a35f9e143658eca9d73816bb6455c88004d22a36354bef0ca8ab9cbf235b41f8381775d90a51fdbb0401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4b03d50c08049cbf235b672f4254432e434f4d2ffabe6d6d26e8bc9ee1e0c41c89a8266a1ff4cb5c825ff2061ec1d7d0929974857b57d0530100000000000000874e3937a6e20e0000000000ffffffff02531dfa4b00000000 ...
This example transaction subscription client is located here.
#!python3
import sys
import zmq
import struct
from binascii import hexlify, unhexlify
# Connect to the public transaction service.
url = 'tcp://mainnet.libbitcoin.net:9094'
if len(sys.argv) > 1:
url = sys.argv[1]
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(url)
socket.setsockopt(zmq.SUBSCRIBE, '')
while True:
# Collect the response in parts.
sequence = struct.unpack('<H', socket.recv())[0]
transaction = hexlify(socket.recv()).decode('ascii')
print '[Seq:{}] Transaction received: {}'.format(sequence, transaction)
Sample output:
[Seq:2553] Transaction received: 010000000238a40507cc84cde92e0bc36189d67522b11e94236255a2b75440a50a8e9a541c380000006b483045022100c7f71e877984f4b3c5865261911ae2e8146ff16517983701524c06df0dd5432d02206f6d9c829016605e4fcf26a7166a99f433106c2167928f6ac56c899ea235033c012103fd6db240fa443f759f3781725a603e71b5b730cc043ab4ffbff4081acb83f690fffffffffbb8ab7a27f22ca5e43b078e72cf140f72193be0fb20ed46789e932ce89371a2360000006a473044022001e24f71bdfe94474496fbfbcd673aa2c488887874d5577f116a15618c5fc81702204095b2a5fe80ac44751b42b1b2a7180d9751e2a7959135739b50386fe6f8c05f012103fd6db240fa443f759f3781725a603e71b5b730cc043ab4ffbff4081acb83f690ffffffff01784b1a00000000001976a914a83264035c612eb24e6070ebd908907bfce5da8288ac00000000
Users | Developers | License | Copyright © 2011-2024 libbitcoin developers
- Home
- Build Server
- Download Server
- Frequently Asked Questions
- General Information
- Client Server Interface
- Configuration Settings
- Tutorials