2

I try to receive messages from an Azure EventHub via Python, unfortunately I am not able to subscribe to it.

My script bases on https://gist.github.com/tomconte/e2a4667185a9bf674f59 and another similar issues was already asked in python script which subscribes/listens to Azure Event Hub?, unfortunately without solving it.

To my setup: Python 2.7.9 (Ubuntu 15.04)

Intstalled qpid-proton via pip:

pip show python-qpid-proton
...
Version: 0.11.1
...

So I am trying the following:

from proton import *
import urllib
key = urllib.quote(FOOBAR,"")
address = "amqps://name:" + key + "@nsname.servicebus.windows.net/eventhubname/ConsumerGroups/$Default/Partitions/0"
messenger = Messenger()
messenger.subscribe(address)

proton.MessengerException: Cannot subscribe to [ADDRESS]

name/key Should be OK since it works within another application.

Any guesses?

Community
  • 1
  • 1
user1816723
  • 53
  • 1
  • 7
  • hi, Seems that it is not necessary to encode the Key. Please try to use the original key from Azure Portal. Any results, please let me know. – Will Shao - MSFT Feb 22 '16 at 08:47
  • thanks for your reply. tried without encoding the key but failed too. in my key there is a "/" which leads to a connection fail: `proton.MessengerException: [-2]: CONNECTION ERROR (name:KeyUntilSlash): getaddrinfo(name, KeyUntilSlash): Servname not supported for ai_socktype` where KeyuntilSlash is the first part of the key, excluding the "/" – user1816723 Feb 22 '16 at 12:42

2 Answers2

1

Another option is to use the latest azure-eventhub Python SDK to receive messages from Event Hub.

azure-eventhub is available on pypi: https://pypi.org/project/azure-eventhub/

you could follow the receive sample code 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.')
Adam Ling
  • 126
  • 5
0

It looks like your key may contain "/" so you may want to go to azure portal and see if you can use the secondary key instead. You might have to create a new "shared access policy"

key shared access policy

AJP
  • 1