mARK bLOORE wrote:
i am trying to connect to a RabbitMQ (AMQP 0.8) server, with username
and password.  taking code from the 'hello-world' example, i do

sock = connect(broker, port)
conn = Connection(sock, qpid.spec.load("qpid-0.5/specs/amqp.0-8.xml"),
username=user, password=password)

and get
AttributeError: Spec instance has no attribute '__getitem__'
from delegates.py line 34.  i note that the traceback includes
connection.py, rather than connection08.py, if that matters.

i have also tried using a different approach, with a Client object,
following code shown at
http://www.balisage.net/Proceedings/html/2008/Robie01/Balisage2008-Robie01.html,
but that produces different errors, which make me think that it is
only good for 0.10.

can anyone tell me how to connect Qpid 0.5 to an AMQP 0.8 server?


The API for connecting is slightly different for pre 0-10 specs. To connect try the following (note that the 0.5 qpid release uses a modified version of the amqp 0-8 spec, so if you are running against rabbit you should use the official version of that specification):

import qpid.client, qpid.spec
import Queue
from qpid.content import Content

client = qpid.client.Client("localhost", 5672, qpid.spec.load("/path/to/amqp.0-8.xml"))
client.start({"LOGIN":"guest","PASSWORD":"guest"})

channel = client.channel(1)
channel.channel_open()

Then you can invoke any AMQP 0-8 methods on the channel. The methods exposed here are dynamically determined based on the spec loaded. Attached is a full hello world example in case that is useful. You may also find the tests under tests_0-8 useful as examples.

FYI there is work underway to offer a high level, protocol independent API for python to avoid having to deal with these differences.

#!/usr/bin/env python

import qpid.client, qpid.spec
import Queue
from qpid.content import Content

client = qpid.client.Client("localhost", 5672, 
qpid.spec.load("/home/gordon/qpid/branches/M2/specs/amqp.0-8.xml"))
client.start({"LOGIN":"guest","PASSWORD":"guest"})
#client.start("\0guest\0guest", mechanism="PLAIN")

channel = client.channel(1)
channel.channel_open()

# Now we can send regular commands. If you want to see what the method
# arguments mean or what other commands are available, you can use the
# python builtin help() method. For example:
#help(chan)
#help(chan.exchange_declare)

# If you want browse the available protocol methods without being
# connected to a live server you can use the amqp-doc utility:
#
#   Usage amqp-doc [<options>] <spec> [<pattern_1> ... <pattern_n>]
#
#   Options:
#       -e, --regexp    use regex instead of glob when matching

# Now that we know what commands are available we can use them to
# interact with the server.

# Here we use ordinal arguments.
channel.exchange_declare(exchange="test", type="direct")

# Here we use keyword arguments.
channel.queue_declare(queue="test-queue")
channel.queue_bind(queue="test-queue", exchange="test", routing_key="key")

# Call Channel.basic_consume to register as a consumer.
# All the protocol methods return a message object. The message object
# has fields corresponding to the reply method fields, plus a content
# field that is filled if the reply includes content. In this case the
# interesting field is the consumer_tag.
reply = channel.basic_consume(queue="test-queue")

# We can use the Client.queue(...) method to access the queue
# corresponding to our consumer_tag.
queue = client.queue(reply.consumer_tag)

# Now lets publish a message and see if our consumer gets it. To do
# this we need to import the Content class.
body = "Hello World!"
channel.basic_publish(exchange="test",
                      routing_key="key",
                      content=Content(body))

# Now we'll wait for the message to arrive. We can use the timeout
# argument in case the server hangs. By default queue.get() will wait
# until a message arrives or the connection to the server dies.
msg = queue.get(timeout=10)
print msg.content.body

# Now acknowledge the message.
channel.basic_ack(msg.delivery_tag, True)

client.close()

---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to