Python

Pika Python AMQP Client Library

pyman 2017. 1. 31. 11:25
#!/usr/bin/env python

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello'
;)

print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

channel.start_consuming()



Receiving.py


#!/usr/bin/env python

import pika


connection = pika.BlockingConnection(pika.ConnectionParameters(

        host='localhost'))

channel = connection.channel()


channel.queue_declare(queue='hello')


print ' [*] Waiting for messages. To exit press CTRL+C'


def callback(ch, method, properties, body):

    print " [x] Received %r" % (body,)


channel.basic_consume(callback,

                      queue='hello',

                      no_ack=True)


channel.start_consuming()



Sending.py


#!/usr/bin/env python

import pika


connection = pika.BlockingConnection(pika.ConnectionParameters(

               'localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',

                      routing_key='hello',

                      body='Hello World!')

print " [x] Sent 'Hello World!'"

connection.close()