kushagra
kushagra

Reputation: 11

How to read message from Azure Service Bus using Python?

I have created a service bus queue with send and listen permission.

Using C# programming language I am able to the read the data from that queue.

But when I am trying the same thing with python I am facing some problems, also I am new with Python and using Azure services with it. Below are the code snippets which are giving me the error.

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

Error

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'

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

Error

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'

I am using Python 3.6, also I installed Azure services using command

pip install azure

I am new to Python and using it with Azure.

Upvotes: 1

Views: 3910

Answers (2)

Nagesh Singh Chauhan
Nagesh Singh Chauhan

Reputation: 784

First, do:

pip install azure-servicebus

from azure.servicebus import ServiceBusClient, ServiceBusMessage

servicebus_namespace = "namespace"
Primary_Key = "xxx"
queue_name = "queue_name"
connstr = "Endpoint=xxx"

servicebus_client = ServiceBusClient.from_connection_string(conn_str=connstr)

To receive the message:

with servicebus_client:
    receiver = servicebus_client.get_queue_receiver(queue_name=queue_name, max_wait_time=5)
    with receiver:
        for msg in receiver:
            print("Received: ", msg)
            receiver.complete_message(msg)

Upvotes: 1

Rithin Chalumuri
Rithin Chalumuri

Reputation: 1839

Those error messages indicate that the python environment you are running your script in couldn't find the azure modules.

You can check if the module is correctly installed and present using the following command for python 3:

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

If the module is not in the list, you can install using:

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

You can then run your script with:

python3 your_script.py

If you have multiple versions of python installed on your computer i.e. python 2.x or python 3.x. You can find more details here.

It is useful to check the script is running in the python environment/instance you expect with all dependencies; as there could be multiple environments and/or multiple interpreters.

Depending on the results of the above; you may need to add correct python path variable in your computer.


Official service bus python documentation/examples can be found here.

Upvotes: 1

Related Questions