python-qpid-proton示例,向azure发送消息不起作用 [英] python-qpid-proton examples, send message to azure not working

查看:117
本文介绍了python-qpid-proton示例,向azure发送消息不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python-qpid-proton版本0.9.1将消息发送到Azure服务总线队列.

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

examples/python/messenger/中的示例接受格式为amqps://<user>:<password>@<server>/<queue name>的地址,因此我可以成功地将消息发送到我在Azure上拥有的队列中.问题是我无法控制很多事情,也就是说,我真的看不到发送是否失败.最终,我想保留这些消息,以防Internet连接暂时断开.

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.

示例代码examples/python/db_send.pyexamples/python/simple_send.py在这方面似乎更有用,因为它们使用MessagingHandler而不是Messenger类.但是,当我运行它们时,出现此错误:

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'

在我看来,它无法正确解析地址.我粘贴了与以前相同的地址.我也将其粘贴到python解释器中,如下所示:

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.

我可以使用MessagingHandler类并指定用户名和密码来将消息发送到远程(例如在Azure上)吗?

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

推荐答案

我尝试运行qpid示例"simple_send.py",但没有任何错误.该示例程序可以请求Azure服务总线,但无法使用AMQPS方案建立发送方连接.

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.

根据MSDF官方文档 https://msdn.microsoft. com/en-us/library/azure/jj841070.aspx ,则可以使用Messenger类将消息发送到Azure Service Bus.

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()

您可以获取共享访问策略名称&键在"Azure服务总线队列"页面的配置"选项卡上.

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

最好的问候.

示例代码(克服不可靠的互联网连接):

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"

如果要成功检查已发送消息的单个,我建议使用Service Bus REST API.成功发送消息后,您可以获得响应状态代码201.关于服务总线REST API,请参考 https://msdn.microsoft .com/en-us/library/azure/hh780786.aspx https://msdn.microsoft.com/library/dn170477.aspx .

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.

这篇关于python-qpid-proton示例,向azure发送消息不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆