首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >物联网集线器事件python

物联网集线器事件python
EN

Stack Overflow用户
提问于 2018-11-11 02:56:37
回答 1查看 160关注 0票数 2

我已经搜索了很多,我正在尝试使用python,使用azure event sdk将接收器连接到我在azure上的iot集线器,但遗憾的是,没有成功,我的接收器告诉我它与客户端连接,但它实际上从未查看过数据

我的接收器是

代码语言:javascript
复制
    #!/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()

我使用的是iot集线器快速入门的标准发件人

EN

回答 1

Stack Overflow用户

发布于 2018-11-12 18:03:00

你拥有的代码确实可以工作,但是有几件事需要你去检查:

  • 如果您使用的是IoT集线器内置事件终结点,请确保使用事件集线器兼容终结点的命名空间名称和事件集线器兼容名称。我测试的地址如下:amqps://ihsuprodbyresXXXXXXnamespace.servicebus.windows.net/iothub-ehub-sample-XXXXX-XXXXXXXXXX

代码语言:javascript
复制
- 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.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53242358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档