python-qpid-proton examples, send message to azure

2019-08-08 01:53发布

问题:

I'm trying to use python-qpid-proton version 0.9.1 to send a message to a Azure Service Bus queue.

The examples in examples/python/messenger/ accept addresses of the form amqps://<user>:<password>@<server>/<queue name>, and I can successfully send messages to the queue I have on Azure with it. The problem with this is that I can't control much of what's going on, namely I can't really see if the sending failed. Eventually I want to persist the messages in case the internet connection goes down temporarily.

The example code examples/python/db_send.py and examples/python/simple_send.py seem to be more useful with this aspects, as they use the MessagingHandler instead of the Messenger class. But when I run them, I get this error:

./simple_send.py -a amqps://send:mxirestofmypassword@testsoton.servicebus.windows.net/queue2
Traceback (most recent call last):
  File "./simple_send.py", line 62, in <module>
    Container(Send(opts.address, opts.messages)).run()
  File "/usr/local/lib/python2.7/dist-packages/proton/reactor.py", line 120, in run
    while self.process(): pass
  File "/usr/local/lib/python2.7/dist-packages/proton/reactor.py", line 143, in proce
    self._check_errors()
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3737, in dis
    ev.dispatch(self.handler)
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3662, in dis
    result = dispatch(handler, type.method, self)
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3551, in dis
    return m(*args)
  File "/usr/local/lib/python2.7/dist-packages/proton/handlers.py", line 416, in on_r
    self.on_start(event)
  File "./simple_send.py", line 36, in on_start
    event.container.create_sender(self.url)
  File "/usr/local/lib/python2.7/dist-packages/proton/reactor.py", line 671, in creat
    session = self._get_session(context)
  File "/usr/local/lib/python2.7/dist-packages/proton/reactor.py", line 634, in _get_
    return self._get_session(self.connect(url=context))
  File "/usr/local/lib/python2.7/dist-packages/proton/reactor.py", line 611, in conne
    if url: connector.address = Urls([url])
  File "/usr/local/lib/python2.7/dist-packages/proton/reactor.py", line 555, in __ini
    self.values = [Url(v) for v in values]
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3851, in __i
    if defaults: self.defaults()
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3894, in def
    self.port = self.port or self.Port(self.scheme)
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3868, in _ge
    return portstr and Url.Port(portstr)
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3812, in __n
    port = super(Url.Port, cls).__new__(cls, cls._port_int(value))
  File "/usr/local/lib/python2.7/dist-packages/proton/__init__.py", line 3833, in _po
    raise ValueError("Not a valid port number or service name: '%s'" % value)
ValueError: Not a valid port number or service name: 'mxitheresto'

Seems to me like it does not parse the address correctly. I pasted the same address as before. And I also pasted it into the python interpreter, like this:

>>> import proton
>>> u = proton.Url("amqps://send:mxirestofmypassword@testsoton.servicebus.windows.net/queue2")
>>> # no error, and I can access all the parameters:
>>> u.port
5671
>>> u.username
send
>>> # ...

It works fine if I use a local connection with no username and password. Gets past this point if I don't use any username and password but obviously does not work as it fails authentication.

Is there any way I can use the MessagingHandler class and specify username and password to send messages to a remote (like on Azure)?

回答1:

I tried to run the qpid example "simple_send.py", I don't got any error. The example program can request the Azure Service Bus, but it can't establish the sender connection with AMQPS scheme.

According to MSDF offical document https://msdn.microsoft.com/en-us/library/azure/jj841070.aspx, you can use the Messenger class to send messages to Azure Service Bus.

The simple completed sample code is as follows:

from proton import Messenger, Message

messenger = Messenger()
message = Message()
message.address = "amqps://<shared_access_policy_name>:<shared_access_policy_key>@pandaservibus.servicebus.windows.net/testqueue"

message.body = u"This is a text string"
messenger.put(message)
messenger.send()

You can get the shared access policy name & key at the tab "CONFIGURE" of Azure Service Bus Queue page.

Best Regards.


Sample code (Overcome unreliable internet connection):

from proton import Messenger, Message
from time import sleep

message = Message()
message.address = "amqps://<shared_access_policy_name>:<shared_access_policy_key>@pandaservibus.servicebus.windows.net/testqueue"
message.body = u"This is a text string"
print('start')
retry = True
print("Send Msg")
while retry:
    try:
        messenger = Messenger()
        messenger.put(message)
        messenger.send()
        retry = False
        print "Sent"
    except:
        print "Retry"
    sleep(2)
print "End"

If you want to check the singal of sent messages successfully, I suggest to use the Service Bus REST API. You can get the response status code 201 when send messages successfully. About Service Bus REST APIs, please refer to https://msdn.microsoft.com/en-us/library/azure/hh780786.aspx and https://msdn.microsoft.com/library/dn170477.aspx.