amr zaki
amr zaki

Reputation: 81

Iot hub event python

I have searched for this quite alot , I am trying to connect a reciever to my iot hub on azure using python , using azure event sdk , but regretably with no success , my reciever tells me it gets connected with the client , but it never actually views the data

my reciever is

    #!/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 partition.
"""
import os
import sys
import logging
import time
from azure.eventhub import EventHubClient, Receiver, Offset

# Address can be in either of these formats:
# "amqps://<URL-encoded-SAS-policy>:<URL-encoded-SAS-key>@<mynamespace>.servicebus.windows.net/myeventhub"
# "amqps://<mynamespace>.servicebus.windows.net/myeventhub"
ADDRESS ="amqps://iothub-ns-virtualab2-926709-043cc22e89.servicebus.windows.net/virtualab2"

# SAS policy and key are not required if they are encoded in the URL
USER = "iothubowner"
KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CONSUMER_GROUP = "teststream"
OFFSET = Offset("-1")
PARTITION = "1"


total = 0
last_sn = -1
last_offset = "-1"
client = EventHubClient(ADDRESS, debug=True, username=USER, password=KEY)

try:
    receiver = client.add_receiver(CONSUMER_GROUP, PARTITION, prefetch=5000, offset=OFFSET)
    client.run()
    print("client connected")
    start_time = time.time()
    print("listening")
    batch = receiver.receive(timeout=5000)

    while batch:
        for event_data in batch:
            last_offset = event_data.offset
            last_sn = event_data.sequence_number
            print("Received: {}, {}".format(last_offset.value, last_sn))
            print(event_data.body_as_str())
            total += 1
        batch = receiver.receive(timeout=5000)

    end_time = time.time()
    client.stop()
    run_time = end_time - start_time
    print("Received {} messages in {} seconds".format(total, run_time))

except KeyboardInterrupt:
    pass
finally:
    client.stop()

and i am using the standard sender from quickstart from iot hub

Upvotes: 2

Views: 219

Answers (1)

Dominic Betts
Dominic Betts

Reputation: 2331

The code you have does work, however there are a couple of things for you to check:

  • If you are using the IoT Hub built-in events endpoint, be sure you are using the namespace name from the Event Hub-compatible endpoint and the Event Hub-compatible name for the name. The address I tested with looks like: amqps://ihsuprodbyresXXXXXXnamespace.servicebus.windows.net/iothub-ehub-sample-XXXXX-XXXXXXXXXX

    • Be sure you are using the iothubowner key value.

    • Be sure the consumer group you're using has been added to the Event Hub-compatible endpoint.

    • By default, an Event Hub-compatible endpoint has two partitions - your code is only listening for messages on one of them.

Upvotes: 1

Related Questions