简体   繁体   English

python-qpid-proton示例,向azure发送消息不起作用

[英]python-qpid-proton examples, send message to azure not working

I'm trying to use python-qpid-proton version 0.9.1 to send a message to a Azure Service Bus queue. 我正在尝试使用python-qpid-proton 0.9.1版将消息发送到Azure Service Bus队列。

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. examples/python/messenger/中的examples/python/messenger/接受amqps://<user>:<password>@<server>/<queue name> ,并且我可以成功地将消息发送到我在Azure上拥有的队列。 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. 最终,我想保留这些消息,以防Internet连接暂时断开。

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. 示例代码examples/python/db_send.pyexamples/python/simple_send.py似乎是更有用的与此方面,因为它们使用MessagingHandler代替的Messenger类。 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: 我也将其粘贴到python解释器中,如下所示:

>>> 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)? 有什么方法可以使用MessagingHandler类并指定用户名和密码来将消息发送到远程(例如在Azure上)?

I tried to run the qpid example "simple_send.py", I don't got any error. 我尝试运行qpid示例“ simple_send.py”,但没有任何错误。 The example program can request the Azure Service Bus, but it can't establish the sender connection with AMQPS scheme. 该示例程序可以请求Azure服务总线,但是无法使用AMQPS方案建立发送方连接。

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. 根据MSDF官方文档https://msdn.microsoft.com/en-us/library/azure/jj841070.aspx ,可以使用Messenger类将消息发送到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. 您可以在Azure Service Bus Queue页面的“配置”选项卡上获取共享访问策略的名称和键。

在此处输入图片说明 在此处输入图片说明 Best Regards. 最好的祝福。


Sample code (Overcome unreliable internet connection): 示例代码(克服不可靠的Internet连接):

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. 如果要成功检查已发送消息的单个,建议使用Service Bus REST API。 You can get the response status code 201 when send messages successfully. 成功发送消息后,您可以获得响应状态代码201。 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 . 关于服务总线REST API,请参阅https://msdn.microsoft.com/zh-cn/library/azure/hh780786.aspxhttps://msdn.microsoft.com/library/dn170477.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM