1

I am interested in listening to an Event Hub from a Python script and executing some code when an inbound message is detected.

On Ubuntu, using Python 2.7 with https://pypi.python.org/pypi/python-qpid-proton/0.10 and the example "receiver" script, I attempt to subscribe to a URI that looks like:

amqps://<key name>:<key>@<namespace>.servicebus.windows.net/<event hub name>/ConsumerGroups/$Default/Partitions/0

The following error is returned:

proton.MessengerException: Cannot subscribe to <...>

I believe that my Event Hub is set up correctly because I am able to send events into it using a separate "send" script and the Azure dashboard shows that events have arrived. But I am unable to connect as a subscriber to receive messages.

RaGe
  • 22,696
  • 11
  • 72
  • 104
J Budmon
  • 21
  • 2

3 Answers3

1

I tried to reproduce your issue on Ubuntu Server, but failed.

Although you said set up your event hub correctly, I still suggest you can check the key name & key again via the CONFIGURE tab page of your event hub on Azure old portal and make sure the policy name key name defined that has the permission Listen in the Shared access policies. Or you can use RootManageSharedAccessKey & its key in the access connection information of your service bus to try again.

Also, you can refer to the sample event_hubs_receive_many.py at https://gist.github.com/tomconte/e2a4667185a9bf674f59 (it based on the proton sample async.py at https://qpid.apache.org/releases/qpid-proton-0.8/messenger/python/examples/async.py.html) to check your code.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
1

There is a newer version of azure-eventhub Python SDK v5 available on pypi.

You could follow the recv sample to receive messages:

#!/usr/bin/env python

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

"""
An example to show receiving events from an Event Hub.
"""
import os
from azure.eventhub import EventHubConsumerClient

CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']


def on_event(partition_context, event):
    # Put your code here.
    # If the operation is i/o intensive, multi-thread will have better performance.
    print("Received event from partition: {}.".format(partition_context.partition_id))


def on_partition_initialize(partition_context):
    # Put your code here.
    print("Partition: {} has been initialized.".format(partition_context.partition_id))


def on_partition_close(partition_context, reason):
    # Put your code here.
    print("Partition: {} has been closed, reason for closing: {}.".format(
        partition_context.partition_id,
        reason
    ))


def on_error(partition_context, error):
    # Put your code here. partition_context can be None in the on_error callback.
    if partition_context:
        print("An exception: {} occurred during receiving from Partition: {}.".format(
            partition_context.partition_id,
            error
        ))
    else:
        print("An exception: {} occurred during the load balance process.".format(error))


if __name__ == '__main__':
    consumer_client = EventHubConsumerClient.from_connection_string(
        conn_str=CONNECTION_STR,
        consumer_group='$Default',
        eventhub_name=EVENTHUB_NAME,
    )

    try:
        with consumer_client:
            consumer_client.receive(
                on_event=on_event,
                on_partition_initialize=on_partition_initialize,
                on_partition_close=on_partition_close,
                on_error=on_error,
                starting_position="-1",  # "-1" is from the beginning of the partition.
            )
    except KeyboardInterrupt:
        print('Stopped receiving.')

If you're already using the v1 SDK, you could follow the migration from v1 to v5 to migrate your program.

Adam Ling
  • 126
  • 5
0

Take a look at https://github.com/Azure/azure-event-hubs-python

Try ./examples/recv.py to see if it works. Note that the SAS key needs to be URL encoded and the associated policy has Listen action defined. ADDRESS needs to be updated from your event hub configuration.

Xin Chen
  • 496
  • 2
  • 3