Created
January 13, 2015 21:56
-
-
Save cnorthwood/323534c9df8230d6bbeb to your computer and use it in GitHub Desktop.
Sample Network Rail Open Data python script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Sample Python connect script. Requires stomp.py: pip install stomp.py | |
""" | |
import logging | |
from time import sleep | |
import stomp | |
NETWORK_RAIL_AUTH = ('username', 'password') | |
console = logging.StreamHandler() | |
console.setFormatter(logging.Formatter('[%(asctime)s] %(name)-12s %(levelname)-8s %(message)s')) | |
logging.getLogger().addHandler(console) | |
logging.getLogger().setLevel(logging.DEBUG) | |
LOGGER = logging.getLogger('vstp') | |
class Listener(object): | |
def __init__(self, mq): | |
self._mq = mq | |
def on_message(self, headers, message): | |
LOGGER.info(headers) | |
LOGGER.info(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=(5000, 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment