Python Examples: Difference between revisions

From Open Rail Data Wiki
Created page with "This uses Python's stomp.py library to connect: pip install stomp.py {code} #!/usr/bin/env python import logging from time import sleep import stomp NETWORK_RAIL_AUTH = ('..."
 
No edit summary
Line 1: Line 1:
This uses Python's stomp.py library to connect: pip install stomp.py
This uses Python's stomp.py library to connect: pip install stomp.py


{code}
#!/usr/bin/env python


import logging
#!/usr/bin/env python
from time import sleep
 
import logging
import stomp
from time import sleep
 
NETWORK_RAIL_AUTH = ('username', 'password')
import stomp
 
class Listener(object):
NETWORK_RAIL_AUTH = ('username', 'password')
 
    def __init__(self, mq):
class Listener(object):
        self._mq = mq
 
    def __init__(self, mq):
    def on_message(self, headers, message):
        self._mq = mq
        print headers
        print message
    def on_message(self, headers, message):
        self._mq.ack(id=headers['message-id'], subscription=headers['subscription'])
        print headers
 
        print message
mq = stomp.Connection(host_and_ports=[('datafeeds.networkrail.co.uk', 61618)],
        self._mq.ack(id=headers['message-id'], subscription=headers['subscription'])
                      keepalive=True,
                      vhost='datafeeds.networkrail.co.uk',
mq = stomp.Connection(host_and_ports=[('datafeeds.networkrail.co.uk', 61618)],
                      heartbeats=(10000, 5000))
                      keepalive=True,
 
                      vhost='datafeeds.networkrail.co.uk',
mq.set_listener('', Listener(mq))
                      heartbeats=(10000, 5000))
 
mq.start()
mq.set_listener('', Listener(mq))
mq.connect(username=NETWORK_RAIL_AUTH[0],
          passcode=NETWORK_RAIL_AUTH[1],
mq.start()
          wait=True)
mq.connect(username=NETWORK_RAIL_AUTH[0],
 
            passcode=NETWORK_RAIL_AUTH[1],
mq.subscribe('/topic/VSTP_ALL', 'test-vstp', ack='client-individual')
            wait=True)
 
while mq.is_connected():
mq.subscribe('/topic/VSTP_ALL', 'test-vstp', ack='client-individual')
    sleep(1)
{code}
while mq.is_connected():
    sleep(1)

Revision as of 22:14, 13 January 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)