yuliji
5/4/2016 - 7:40 AM

python rabbit listener

python rabbit listener

#!/usr/bin/env python
import pika
import json

cred = pika.credentials.PlainCredentials(username='', password='')
connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='', credentials = cred))
channel = connection.channel()


result = channel.queue_declare(queue='sio_py_sio3', exclusive=True)
queue_name = result.method.queue

channel.queue_bind(exchange='ticker',
                   queue=queue_name)

#print(' [*] Waiting for ticker. To exit press CTRL+C')

def callback(ch, method, properties, body):
#    print body
    f = open("/tmp/rabbit_python", "w")
    ticker = json.loads(body)
    date = ticker["date"]
    f.write(str(date)+"\n")
    f.flush()
    f.close()

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()