我正在尝试使用 python-qpid-proton 版本 0.9.1 向 Azure 服务总线队列发送消息。
examples/python/messenger/
中的示例接受 amqps://<user>:<password>@<server>/<queue name>
形式的地址,我可以使用它成功地将消息发送到 Azure 上的队列。问题是我无法控制正在发生的事情,即我无法真正看到发送是否失败。最终我想保留这些消息,以防互联网连接暂时中断。
示例代码examples/python/db_send.py
和examples/python/simple_send.py
似乎在这方面更有用,因为他们使用 MessagingHandler
而不是 Messenger
类(class)。但是当我运行它们时,我收到此错误:
./simple_send.py -a amqps://send:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d1005140f180e09121b10040d1c0e0e0a120f193d09180e090e12091213530e180f0b141e181f080e530a141319120a0e53131809" rel="noreferrer noopener nofollow">[email protected]</a>/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 解释器中,如下所示:
>>> import proton
>>> u = proton.Url("amqps://send:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14796c7d667167607b72796d64756767637b66705460716760677b607b7a3a677166627d77717661673a637d7a707b63673a7a7160" rel="noreferrer noopener nofollow">[email protected]</a>/queue2")
>>> # no error, and I can access all the parameters:
>>> u.port
5671
>>> u.username
send
>>> # ...
如果我使用没有用户名和密码的本地连接,它工作正常。如果我不使用任何用户名和密码,但显然不起作用,因为它无法通过身份验证,就可以通过这一点。
有什么办法可以使用 MessagingHandler
类并指定用户名和密码以将消息发送到远程(例如在 Azure 上)?
最佳答案
我尝试运行 qpid 示例“simple_send.py”,没有任何错误。示例程序可以请求Azure服务总线,但无法与AMQPS方案建立发送方连接。
根据MSDF官方文件https://msdn.microsoft.com/en-us/library/azure/jj841070.aspx ,您可以使用 Messenger 类向 Azure 服务总线发送消息。
简单完成的示例代码如下:
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服务总线队列页面的“配置”选项卡中获取共享访问策略名称和 key 。
<小时/>示例代码(克服不可靠的互联网连接):
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。关于Service Bus REST API,请引用https://msdn.microsoft.com/en-us/library/azure/hh780786.aspx和 https://msdn.microsoft.com/library/dn170477.aspx .
关于python-qpid-proton 示例,向 azure 发送消息不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32189596/