Python Examples: Difference between revisions

From Open Rail Data Wiki
Q (talk | contribs)
m Added category
Q (talk | contribs)
m Added category
Line 39: Line 39:


[[Category:Network Rail Data Feeds]]
[[Category:Network Rail Data Feeds]]
[[Category:Example Code]]

Revision as of 21:03, 11 July 2015

This uses Python's stomp.py library to connect: pip install stomp.py


#!/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)