Lesen von Events von Azure Iot Hub in Python

Zum testen ob die Nachrichten/Events wirklich im IoT Hub ankommen.

from azure.eventhub import EventHubConsumerClient

# Connection string for the IoT Hub-compatible endpoint
CONNECTION_STR = "Endpoint=sb://iothub-ns-iothub0caf-24940406-8dd0ed6476.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=dFWBWCZaSwZqwX9JsRwwfLvLErL3SuxUGQZhfBOSKsY=;EntityPath=iothub0caf679ca31b41b1b4"
EVENTHUB_NAME  = "iothub0caf679ca31b41b1b4"
# Consumer group to use for the consumer client
CONSUMER_GROUP  = "$Default"

# Define callback function for handling received events
def on_event(partition_context, event):
    print("Received event from partition: {}".format(partition_context.partition_id))
    print(event.body_as_str())

# Create an instance of the EventHubConsumerClient
client = EventHubConsumerClient.from_connection_string(
    conn_str=CONNECTION_STR,
    consumer_group=CONSUMER_GROUP,
    eventhub_name=EVENTHUB_NAME
)

# Create a new event hub consumer for each partition and start receiving events
with client:
    for partition in client.get_partition_ids():
        client.receive(on_event=on_event, partition_id=partition)

Last updated