beautypg.com

HP OneView User Manual

Page 194

background image

Example 7 Python code example (amqplib)

#!/usr/bin/env python

from optparse import OptionParser
from functools import partial

import amqp

def callback(channel, msg):
for key, val in msg.properties.items():
print ('%s: %s' % (key, str(val)))
for key, val in msg.delivery_info.items():
print ('> %s: %s' % (key, str(val)))

print ('')
print (msg.body)
print ('-------')
print msg.delivery_tag
channel.basic_ack(msg.delivery_tag)

#
# Cancel this callback
#
if msg.body == 'quit':
channel.basic_cancel(msg.consumer_tag)

def main():
parser = OptionParser()
parser.add_option('--host', dest='host',
help='AMQP server to connect to (default: %default)',
default='localhost',
)

options, args = parser.parse_args()
ssl_options = ({"ca_certs": "caroot.pem",
"certfile": "client.pem",
"keyfile": "key.pem",
# "cert_reqs": CERT_REQUIRED,
"server_side": False})

print ('Connecting to host %s' %options.host)

conn = amqp.Connection(options.host, login_method='EXTERNAL',
ssl=ssl_options)

print ('Successfully connected, creating and binding to queue')

ch = conn.channel()

qname, _, _ = ch.queue_declare()
ch.queue_bind(qname, 'scmb', 'scmb.#')
ch.basic_consume(qname, callback=partial(callback, ch))

print ('Successfully bound to queue, waiting for messages')

#pyamqp://

#
# Loop as long as the channel has callbacks registered
#
while ch.callbacks:
ch.wait()

194

Using the State-Change Message Bus (SCMB)