-
Notifications
You must be signed in to change notification settings - Fork 7
/
rippled_websocket.py
109 lines (94 loc) · 3.6 KB
/
rippled_websocket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'''
This script provides an example/template using python3 to connect to a rippled
node via websocket (WS). It's designed to get novice developers subscribed and engaged with
minimal effort. In order to subscribe, WS must be configured in the remote node's rippled.cfg file.
This script uses the 'websocket' module, which is available via pip.
This script is based on this example: https://github.com/websocket-client/websocket-client
This is offered 'as is'. Users will likely need to modify the script to meet their
individual needs.
Subscriptions provide ongoing information on the XRP Ledger's state, including transactions.
'''
import sys
import ssl # This is only used to disable wss cert verification.
import json
import websocket # 'pip install websocket-client'
# Specify the server to connect to
WS_ADDRESS = "wss://xrplcluster.com:443"
# Tailor subscriptions here
# Subscriptions are described in the rippled documentation:
# https://ripple.com/build/rippled-apis/#subscribe
LEDGER = {"id": "example_ws", "command": "subscribe", "streams": ["ledger"]}
SERVER = {"id": "example_ws", "command": "subscribe", "streams": ["server"]}
VALIDATIONS = {"id": "example_ws", "command": "subscribe", "streams": ["validations"]}
TRANSACTIONS = {"id": "example_ws", "command": "subscribe", "streams": ["transactions", "transactions_proposed"]}
CONSENSUS = {"id": "example_ws", "command": "subscribe", "streams": ["consensus"]}
MANIFESTS = {"id": "example_ws", "command": "subscribe", "streams": ["manifests"]}
BOOKS = {
"command": "subscribe",
"books": [
{
"taker_pays": {
"currency": "XRP"
},
"taker_gets": {
"currency": "USD",
"issuer": "rhub8VRN55s94qWKDv6jmDy1pUykJzF3wq"
},
"snapshot": "true"
},
{
"taker_pays": {
"currency": "USD",
"issuer": "rhub8VRN55s94qWKDv6jmDy1pUykJzF3wq"
},
"taker_gets": {
"currency": "XRP"
},
"snapshot": "true"
},
]
}
# Use the following variable to define which subscription to use
WS_COMMAND = json.dumps(MANIFESTS)
class Ws:
'''
Open a websocket connection, send the subscription command,
and parse messages from the web server.
'''
def __init__(self):
self.websocket_launch()
def on_message(self, ws, message):
message = json.loads(message)
print(json.dumps(message, indent=4, sort_keys=True))
def on_error(self, ws, error):
print("Error:", error)
def on_close(self, ws, close_status_code, close_msg):
print(f"WS connection closed. Status code: {close_status_code}.\nMessage: {close_msg}")
def on_open(self, ws):
'''
Initial command to send after opening the websocket.
'''
self.socket.send(WS_COMMAND)
def websocket_launch(self):
'''
Construct and open the websocket connection.
To disable ssl verification:
1. Comment/remove the first 'ws.run_forever()' line below
2. Uncomment the second line that starts with 'ws.run_forever(sslopt=...'
'''
self.socket = websocket.WebSocketApp(
WS_ADDRESS,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
on_open=self.on_open,
)
try:
self.socket.run_forever()
#self.socket.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
except KeyboardInterrupt:
sys.exit()
try:
Ws()
except KeyboardInterrupt:
sys.exit()