gpt4 book ai didi

python-3.x - 如何使用Python从Azure服务总线读取消息?

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:38 24 4
gpt4 key购买 nike

我创建了一个具有发送和监听权限的服务总线队列。

使用 C# 编程语言,我可以从该队列中读取数据。

但是当我用 python 尝试同样的事情时,我遇到了一些问题,而且我对 Python 和使用 Azure 服务都是新手。下面是给我错误的代码片段。

代码片段 1

from azure.servicebus import QueueClient, Message

# Create the QueueClient
queue_client = QueueClient.from_connection_string(
"<CONNECTION STRING>", "<QUEUE NAME>")

# Receive the message from the queue
with queue_client.get_receiver() as queue_receiver:
messages = queue_receiver.fetch_next(timeout=3)
for message in messages:
print(message)
message.complete()

错误

Traceback (most recent call last):
File "C:\installs\readBus1.py", line 1, in <module>
from azure.servicebus import QueueClient, Message
ImportError: cannot import name 'QueueClient'

代码片段 2

from azure.servicebus.control_client import ServiceBusService, Message, Topic, Rule, DEFAULT_RULE_NAME

bus_service = ServiceBusService(
service_namespace='<NameSpace>',
shared_access_key_name='<KeyName>',
shared_access_key_value='<ConnectionString>')


msg = bus_service.receive_subscription_message('fileupload', 'AllMessages', peek_lock=True)
if msg.body is not None:
print(msg.body)
msg.delete()

错误

Traceback (most recent call last):
File "C:\installs\readBus1.py", line 2, in <module>
from azure.servicebus.control_client import ServiceBusService, Message, Topic, Rule, DEFAULT_RULE_NAME
ModuleNotFoundError: No module named 'azure.servicebus.control_client'

我使用的是Python 3.6,我还使用命令安装了Azure服务

pip install azure

我是 Python 新手,并将其与 Azure 一起使用。

最佳答案

这些错误消息表明您运行脚本的 python 环境找不到 azure 模块。

您可以使用以下针对 python 3 的命令检查模块是否正确安装并存在:

pip3 list #Show all installed packages.
pip3 show azure # Show installed version, location details etc.

如果该模块不在列表中,您可以使用以下命令安装:

pip3 install azure
pip3 install azure-servicebus #If you want to install only service bus.

然后您可以使用以下命令运行脚本:

python3 your_script.py

如果您的计算机上安装了多个版本的 python,即 python 2.x 或 python 3.x。您可以查看更多详情here .

检查脚本是否在您期望的 python 环境/实例以及所有依赖项中运行是很有用的;因为可能有多个环境和/或多个解释器。

取决于上述结果;您可能需要add correct python path variable in your computer .

<小时/>

官方服务总线python文档/示例可以找到here .

关于python-3.x - 如何使用Python从Azure服务总线读取消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58529262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com