読者です 読者をやめる 読者になる 読者になる

一人 AMQP 勉強会 ... というかメモ

ampq

pyamqplib のサンプルを写経しただけ。

# 受信
from amqplib import client_0_8 as amqp

def callback(message):
  message.properties
  message.delivery_info
  message.body
  message.channel.basic_ack(message.delivery_tag)
  if message.body == 'quit':
    message.channel.basic_cancel(message.consumer_tag)

connect = amqp.Connection('127.0.0.1', userid='test', password='pass', ssl=True)
channel = connect.channel()
channel.access_request('/data', active=True, read=True)
channel.exchange_declare('spam', 'fanout', auto_delete=True)
queue_name, __count, _consumer_count = channel.queue_declare()
channel.queue_bind(queue_name, 'spam')
channel.basic_consume(queue_name, callback=callback)

while channel.callbacks:
  channel.wait()
channel.close()
connect.close()
# 送信
from amqplib import client_0_8 as amqp

message_body = 'aiueo'

connect = amqp.Connection('127.0.0.1', userid='test', password='pass', ssl=True)
channel = connect.channel()
channel.access_request('/data', active=True, write=True)
channel.exchange_declare('spam', 'fanout', auto_delete=True)
message = amqp.Message(message_body, content_type='text/plain',
    application_headers=dict(spam=1, eggs='bacon'))
channel.basic_publish(message, 'spam')
channel.close()
connect.close()