本文整理汇总了Python中websocket_server.WebsocketServer.block_watchers方法的典型用法代码示例。如果您正苦于以下问题:Python WebsocketServer.block_watchers方法的具体用法?Python WebsocketServer.block_watchers怎么用?Python WebsocketServer.block_watchers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类websocket_server.WebsocketServer
的用法示例。
在下文中一共展示了WebsocketServer.block_watchers方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: serve
# 需要导入模块: from websocket_server import WebsocketServer [as 别名]
# 或者: from websocket_server.WebsocketServer import block_watchers [as 别名]
def serve(port, host):
from websocket_server import WebsocketServer
from threading import Thread, Event
import signal
def message_received(client, server, message):
print 'message_received:', message
cmds = message.split('|')
for cmd in cmds:
if cmd.startswith('addr='):
address = cmd[5:]
if server.watched_addresses.has_key(address):
server.watched_addresses[address].append(client)
else:
server.watched_addresses[address] = [client]
if cmd == 'blocks':
server.block_watchers.append(client)
def client_left(client, server):
print 'client_left:', client
addrs = []
for key in server.watched_addresses:
if client in server.watched_addresses[key]:
addrs.append(key)
for addr in addrs:
clients = server.watched_addresses[addr]
clients.remove(client)
if not clients:
del server.watched_addresses[addr]
if client in server.block_watchers:
server.block_watchers.remove(client)
def service_thread(ws_server, evt):
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from bitrisk.bitcoind_config import read_default_config
import json
import decimal
config = read_default_config()
testnet = ''
if config.has_key('testnet'):
testnet = config['testnet']
rpc_user = config['rpcuser']
rpc_password = config['rpcpassword']
rpc_connection = AuthServiceProxy("http://%s:%[email protected]%s:%s8332"%(rpc_user, rpc_password, host, testnet))
conn = sqlite3.connect(db_filename)
while not evt.wait(5):
txs = get_db_txs(conn)
for tx in txs:
print 'tx:', tx
tx = rpc_connection.gettransaction(tx)
for details in tx['details']:
addr = details['address']
if ws_server.watched_addresses.has_key(addr):
def decimal_default(obj):
if isinstance(obj, decimal.Decimal):
return float(obj)
raise TypeError
msg = json.dumps(tx, default=decimal_default)
for client in ws_server.watched_addresses[addr]:
ws_server.send_message(client, msg)
blocks = get_db_blocks(conn)
for block in blocks:
print 'block:', block
for client in ws_server.block_watchers:
ws_server.send_message(client, block)
server = WebsocketServer(port, host)
server.watched_addresses = {}
server.block_watchers = []
server.set_fn_message_received(message_received)
server.set_fn_client_left(client_left)
evt = Event()
thread = Thread(target=service_thread, args=(server, evt))
thread.start()
server.run_forever() # catches and exits on SIGINT
evt.set() # stop service_thread
thread.join()