22# | This source code is provided under the Apache 2.0 license --
33# | and is provided AS IS with no warranty or guarantee of fit for purpose. --
44# | See the project's LICENSE.md for details. --
5- # | Copyright Refinitiv 2020 . All rights reserved. --
5+ # | Copyright LSEG 2025 . All rights reserved. --
66# |-----------------------------------------------------------------------------
77
88
99#!/usr/bin/env python
10- """ Simple example of posting Market Price JSON data To RCC via Refinitiv Real-Time Distribution System 3.2.x using Websockets """
10+ """ Simple example of posting Market Price JSON data To RCC via LSEG Real-Time Distribution System 3.2.x using Websockets """
1111
1212import sys
1313import time
2020from threading import Thread , Event
2121
2222# Global Default Variables
23- hostname = '127.0.0.1 '
23+ hostname = 'ADS_HOST '
2424port = '15000'
2525user = 'root'
2626app_id = '256'
@@ -43,30 +43,30 @@ def process_message(ws, message_json): # Process all incoming messages.
4343 """ Parse at high level and output JSON of message """
4444 message_type = message_json ['Type' ]
4545
46- if message_type == " Refresh" :
46+ if message_type == ' Refresh' :
4747 if 'Domain' in message_json :
4848 message_domain = message_json ['Domain' ]
49- if message_domain == " Login" :
49+ if message_domain == ' Login' :
5050 process_login_response (ws , message_json )
51- elif message_type == " Ping" :
51+ elif message_type == ' Ping' :
5252 pong_json = {'Type' : 'Pong' }
5353 ws .send (json .dumps (pong_json ))
54- print (" SENT:" )
54+ print (' SENT:' )
5555 print (json .dumps (pong_json , sort_keys = True ,
5656 indent = 2 , separators = (',' , ':' )))
5757
58- """ If our TRI stream is now open, we can start sending posts. """
58+ # If our TRI stream is now open, we can start sending posts.
5959 global next_post_time
6060 if ('ID' in message_json and message_json ['ID' ] == 1 and next_post_time == 0 and
61- (not 'State' in message_json or message_json ['State' ]['Stream' ] == " Open" and message_json ['State' ]['Data' ] == "Ok" )):
61+ (not 'State' in message_json or message_json ['State' ]['Stream' ] == ' Open' and message_json ['State' ]['Data' ] == 'Ok' )):
6262 next_post_time = time .time () + 3
6363 print ('Here' )
6464
6565
6666# Process incoming Login Refresh Response message.
6767def process_login_response (ws , message_json ):
6868 """ Send Off-Stream Post """
69- print (" Sending Off-Stream Post to Real-Time Advanced Distribution Server" )
69+ print (' Sending Off-Stream Post to Real-Time Advanced Distribution Server' )
7070 send_market_price_post (ws )
7171
7272
@@ -76,55 +76,55 @@ def send_market_price_post(ws):
7676 global bid_value
7777 global ask_value
7878 global primact_1_value
79- """ Send a post message contains a market-price content to RCC """
79+ # Send a post message contains a market-price content to RCC
8080
81- """ Contribution fields """
81+ # Contribution fields
8282 contribution_fields = {
83- " BID" : bid_value ,
84- " ASK" : ask_value ,
85- " PRIMACT_1" : primact_1_value
83+ ' BID' : bid_value ,
84+ ' ASK' : ask_value ,
85+ ' PRIMACT_1' : primact_1_value
8686 }
8787
88- """ OMM Post msg Key """
88+ # OMM Post msg Key
8989 mp_post_key = {
90- " Name" : post_item_name ,
91- " Service" : service_name
90+ ' Name' : post_item_name ,
91+ ' Service' : service_name
9292 }
9393
94- """ OMM Post Payload """
94+ # OMM Post Payload
9595 contribution_payload_json = {
96- "ID" : 0 ,
97- " Type" : " Update" ,
98- " Domain" : " MarketPrice" ,
99- " Fields" : contribution_fields ,
100- " Key" : {}
96+ 'ID' : 0 ,
97+ ' Type' : ' Update' ,
98+ ' Domain' : ' MarketPrice' ,
99+ ' Fields' : contribution_fields ,
100+ ' Key' : {}
101101 }
102102
103- """ OMM Off-Stream Post message """
103+ # OMM Off-Stream Post message
104104 mp_post_json_offstream = {
105- " Domain" : " MarketPrice" ,
106- " Ack" : True ,
107- " PostID" : post_id ,
108- " PostUserInfo" : {
109- " Address" : position ,
110- " UserID" : int (app_id )
105+ ' Domain' : ' MarketPrice' ,
106+ ' Ack' : True ,
107+ ' PostID' : post_id ,
108+ ' PostUserInfo' : {
109+ ' Address' : position ,
110+ ' UserID' : int (app_id )
111111 },
112- " Key" : {},
113- " Message" : {},
114- " Type" : " Post" ,
115- "ID" : login_id
112+ ' Key' : {},
113+ ' Message' : {},
114+ ' Type' : ' Post' ,
115+ 'ID' : login_id
116116 }
117117
118- contribution_payload_json [" Key" ] = mp_post_key
119- mp_post_json_offstream [" Key" ] = mp_post_key
120- mp_post_json_offstream [" Message" ] = contribution_payload_json
118+ contribution_payload_json [' Key' ] = mp_post_key
119+ mp_post_json_offstream [' Key' ] = mp_post_key
120+ mp_post_json_offstream [' Message' ] = contribution_payload_json
121121
122122 ws .send (json .dumps (mp_post_json_offstream ))
123- print (" SENT:" )
123+ print (' SENT:' )
124124 print (json .dumps (mp_post_json_offstream ,
125125 sort_keys = True , indent = 2 , separators = (',' , ':' )))
126126
127- """ increase post data value """
127+ # increase post data value
128128 post_id += 1
129129 bid_value += 1
130130 ask_value += 1
@@ -151,13 +151,13 @@ def send_login_request(ws):
151151 login_json ['Key' ]['Elements' ]['Position' ] = position
152152
153153 ws .send (json .dumps (login_json ))
154- print (" SENT:" )
154+ print (' SENT:' )
155155 print (json .dumps (login_json , sort_keys = True , indent = 2 , separators = (',' , ':' )))
156156
157157
158158def on_message (ws , message ):
159159 """ Called when message received, parse message into JSON for processing """
160- print (" RECEIVED: " )
160+ print (' RECEIVED: ' )
161161 message_json = json .loads (message )
162162 print (json .dumps (message_json , sort_keys = True , indent = 2 , separators = (',' , ':' )))
163163
@@ -170,56 +170,56 @@ def on_error(ws, error):
170170 print (error )
171171
172172
173- def on_close (ws ):
173+ def on_close (ws , close_status_code , close_msg ):
174174 """ Called when websocket is closed """
175175 global web_socket_open
176- print (" WebSocket Closed" )
176+ print (f' WebSocket Closed: { close_status_code } { close_msg } ' )
177177 web_socket_open = False
178178
179179
180180def on_open (ws ):
181181 """ Called when handshake is complete and websocket is open, send login """
182182
183- print (" WebSocket successfully connected!" )
183+ print (' WebSocket successfully connected!' )
184184 global web_socket_open
185185 web_socket_open = True
186186 send_login_request (ws )
187187
188188
189- if __name__ == " __main__" :
189+ if __name__ == ' __main__' :
190190
191191 # Get command line parameters
192192 try :
193- opts , args = getopt .getopt (sys .argv [1 :], "" , [
194- " help" , " hostname=" , " port=" , " app_id=" , " user=" , " position=" , " item=" , " service=" ])
193+ opts , args = getopt .getopt (sys .argv [1 :], '' , [
194+ ' help' , ' hostname=' , ' port=' , ' app_id=' , ' user=' , ' position=' , ' item=' , ' service=' ])
195195 print (opts )
196196 except getopt .GetoptError :
197197 print (
198198 'Usage: market_price.py [--hostname hostname] [--port port] [--app_id app_id] [--user user] [--position position] [--item post item name] [--service TRCC Post Service] [--help] ' )
199199 sys .exit (2 )
200200 for opt , arg in opts :
201- if opt in (" --help" ):
201+ if opt in (' --help' ):
202202 print (
203203 'Usage: market_price.py [--hostname hostname] [--port port] [--app_id app_id] [--user user] [--position position] [--item post item name] [--service TRCC Post Service] [--help]' )
204204 sys .exit (0 )
205- elif opt in (" --hostname" ):
205+ elif opt in (' --hostname' ):
206206 hostname = arg
207- elif opt in (" --port" ):
207+ elif opt in (' --port' ):
208208 port = arg
209- elif opt in (" --app_id" ):
209+ elif opt in (' --app_id' ):
210210 app_id = arg
211- elif opt in (" --user" ):
211+ elif opt in (' --user' ):
212212 user = arg
213- elif opt in (" --position" ):
213+ elif opt in (' --position' ):
214214 position = arg
215- elif opt in (" --item" ):
215+ elif opt in (' --item' ):
216216 post_item_name = arg
217- elif opt in (" --service" ):
217+ elif opt in (' --service' ):
218218 service_name = arg
219219
220220 # Start websocket handshake
221- ws_address = " ws://{}:{}/WebSocket" . format ( hostname , port )
222- print (" Connecting to WebSocket " + ws_address + " ..." )
221+ ws_address = f' ws://{ hostname } :{ port } /WebSocket'
222+ print (f' Connecting to WebSocket { ws_address } .... ' )
223223 web_socket_app = websocket .WebSocketApp (ws_address , header = ['User-Agent: Python' ],
224224 on_message = on_message ,
225225 on_error = on_error ,
0 commit comments