Difference between revisions of "Python Examples"

From Open Rail Data Wiki
Jump to navigation Jump to search
m (Added category)
m (Rewrite and point to https://github.com/openraildata/stomp-client-python)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This uses Python's stomp.py library to connect: pip install stomp.py
+
Python is a good choice for connecting as it's lightweight, available on many platforms and very well-used.
  
 
+
Sample code for Python is available at [https://github.com/openraildata/stomp-client-python on the openraildata organization on GitHub].
#!/usr/bin/env python
 
 
import logging
 
from time import sleep
 
 
import stomp
 
 
NETWORK_RAIL_AUTH = ('username', 'password')
 
 
class Listener(object):
 
 
    def __init__(self, mq):
 
        self._mq = mq
 
 
    def on_message(self, headers, message):
 
        print headers
 
        print message
 
        self._mq.ack(id=headers['message-id'], subscription=headers['subscription'])
 
 
mq = stomp.Connection(host_and_ports=[('datafeeds.networkrail.co.uk', 61618)],
 
                      keepalive=True,
 
                      vhost='datafeeds.networkrail.co.uk',
 
                      heartbeats=(10000, 5000))
 
 
mq.set_listener('', Listener(mq))
 
 
mq.start()
 
mq.connect(username=NETWORK_RAIL_AUTH[0],
 
            passcode=NETWORK_RAIL_AUTH[1],
 
            wait=True)
 
 
mq.subscribe('/topic/VSTP_ALL', 'test-vstp', ack='client-individual')
 
 
while mq.is_connected():
 
    sleep(1)
 
  
 
[[Category:Network Rail Data Feeds]]
 
[[Category:Network Rail Data Feeds]]
 +
[[Category:Example Code]]

Latest revision as of 13:37, 28 September 2020

Python is a good choice for connecting as it's lightweight, available on many platforms and very well-used.

Sample code for Python is available at on the openraildata organization on GitHub.