尝试连接到 Azure IoT 中心时出现权限被拒绝错误
Permission denied error while trying to connect to Azure IoT Hub
上下文是通过快速打包应用程序来应用下一页中描述的场景,以便我可以在不同的 linux 发行版(例如 Ubuntu Core)中使用它
https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-python
当我通过终端将应用程序作为脚本执行时,一切都按预期运行。创建快照并尝试执行它后,出现以下错误:
Unexpected error in ._run_op() call
Traceback (most recent call last):
File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/pipeline/pipeline_stages_base.py", line 102, in run_op
self._run_op(op)
File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/pipeline/pipeline_thread.py", line 198, in wrapper
return func(*args, **kwargs)
File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/pipeline/pipeline_stages_mqtt.py", line 127, in _run_op
proxy_options=self.pipeline_root.pipeline_configuration.proxy_options,
File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/mqtt_transport.py", line 133, in __init__
self._mqtt_client = self._create_mqtt_client()
File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/mqtt_transport.py", line 154, in _create_mqtt_client
client_id=self._client_id, clean_session=False, protocol=mqtt.MQTTv311
File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/paho/mqtt/client.py", line 566, in __init__
self._sockpairR, self._sockpairW = _socketpair_compat()
File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/paho/mqtt/client.py", line 270, in _socketpair_compat
socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
File "/snap/send-telemetry-sample/x1/usr/lib/python3.6/socket.py", line 144, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 13] Permission denied
申请代码
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
import random
import time
import os
# Using the Python Device SDK for IoT Hub:
# https://github.com/Azure/azure-iot-sdk-python
# The sample connects to a device-specific MQTT endpoint on your IoT Hub.
from azure.iot.device import IoTHubDeviceClient, Message
# The device connection string to authenticate the device with your IoT hub.
CONNECTION_STRING = "some connection string"
# Define the JSON message to send to IoT Hub.
TEMPERATURE = 20.0
HUMIDITY = 60
MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}}}'
def main():
try:
# Create an IoT Hub client
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )
while True:
# Build the message with simulated telemetry values.
temperature = TEMPERATURE + (random.random() * 15)
humidity = HUMIDITY + (random.random() * 20)
msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity)
message = Message(msg_txt_formatted)
# Add a custom application property to the message.
# An IoT hub can filter on these properties without access to the message body.
if temperature > 30:
message.custom_properties["temperatureAlert"] = "true"
else:
message.custom_properties["temperatureAlert"] = "false"
# Send the message.
print( "Sending message: {}".format(message) )
client.send_message(message)
print ( "Message successfully sent" )
time.sleep(1)
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" )
if __name__ == '__main__':
print ( "IoT Hub Quickstart #1 - Simulated device" )
print ( "Press Ctrl-C to exit" )
main()
这是snapcraft.yaml
name: send-telemetry-sample
base: core18
version: '1.0'
summary: Send telemetry to Azure IoT Hub
description: |
This is a sample application for sending randomly created temperature and humidity
data to Azure IoT Hub.
grade: stable
confinement: strict
apps:
sendTel:
command: bin/send-telemetry-sample.sendtel
parts:
sendtel:
plugin: python
python-version: python3
source: .
还有这个setup.py
from setuptools import setup, find_packages
import sys,os
setup(
name = 'send-telemetry-sample',
version = '1.0.0',
description = 'Send telemetry data to Azure IoT Hub',
license='GPL v3',
author = 'Eltjon Sulaj',
packages = find_packages(),
install_requires=['azure-iot-device'],
entry_points = {
'console_scripts': [
'send-telemetry-sample.sendtel=src.SimulatedDevice:main']
},
classifiers = [
'Programming Language :: Python :: 3.8',
'Operating System :: OS Independent',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)']
)
所有这些都在 Hyper-V 中的 Ubuntu 20.04 虚拟机中运行。
有人对 IoT 快照有一些经验吗?
已解决!
出了问题的是,除非在 snapcraft.yaml 的应用程序元数据中添加了一个接口,否则具有 'strict' 限制的快照无法访问网络资源。
此页面中的更多信息:
https://snapcraft.io/docs/snapcraft-interfaces
上下文是通过快速打包应用程序来应用下一页中描述的场景,以便我可以在不同的 linux 发行版(例如 Ubuntu Core)中使用它
https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-python
当我通过终端将应用程序作为脚本执行时,一切都按预期运行。创建快照并尝试执行它后,出现以下错误:
Unexpected error in ._run_op() call Traceback (most recent call last): File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/pipeline/pipeline_stages_base.py", line 102, in run_op self._run_op(op) File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/pipeline/pipeline_thread.py", line 198, in wrapper return func(*args, **kwargs) File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/pipeline/pipeline_stages_mqtt.py", line 127, in _run_op proxy_options=self.pipeline_root.pipeline_configuration.proxy_options, File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/mqtt_transport.py", line 133, in __init__ self._mqtt_client = self._create_mqtt_client() File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/azure/iot/device/common/mqtt_transport.py", line 154, in _create_mqtt_client client_id=self._client_id, clean_session=False, protocol=mqtt.MQTTv311 File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/paho/mqtt/client.py", line 566, in __init__ self._sockpairR, self._sockpairW = _socketpair_compat() File "/snap/send-telemetry-sample/x1/lib/python3.6/site-packages/paho/mqtt/client.py", line 270, in _socketpair_compat socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) File "/snap/send-telemetry-sample/x1/usr/lib/python3.6/socket.py", line 144, in __init__ _socket.socket.__init__(self, family, type, proto, fileno) PermissionError: [Errno 13] Permission denied
申请代码
# Copyright (c) Microsoft. All rights reserved. # Licensed under the MIT license. See LICENSE file in the project root for full license information. import random import time import os # Using the Python Device SDK for IoT Hub: # https://github.com/Azure/azure-iot-sdk-python # The sample connects to a device-specific MQTT endpoint on your IoT Hub. from azure.iot.device import IoTHubDeviceClient, Message # The device connection string to authenticate the device with your IoT hub. CONNECTION_STRING = "some connection string" # Define the JSON message to send to IoT Hub. TEMPERATURE = 20.0 HUMIDITY = 60 MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}}}' def main(): try: # Create an IoT Hub client client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING) print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" ) while True: # Build the message with simulated telemetry values. temperature = TEMPERATURE + (random.random() * 15) humidity = HUMIDITY + (random.random() * 20) msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity) message = Message(msg_txt_formatted) # Add a custom application property to the message. # An IoT hub can filter on these properties without access to the message body. if temperature > 30: message.custom_properties["temperatureAlert"] = "true" else: message.custom_properties["temperatureAlert"] = "false" # Send the message. print( "Sending message: {}".format(message) ) client.send_message(message) print ( "Message successfully sent" ) time.sleep(1) except KeyboardInterrupt: print ( "IoTHubClient sample stopped" ) if __name__ == '__main__': print ( "IoT Hub Quickstart #1 - Simulated device" ) print ( "Press Ctrl-C to exit" ) main()
这是snapcraft.yaml
name: send-telemetry-sample base: core18 version: '1.0' summary: Send telemetry to Azure IoT Hub description: | This is a sample application for sending randomly created temperature and humidity data to Azure IoT Hub. grade: stable confinement: strict apps: sendTel: command: bin/send-telemetry-sample.sendtel parts: sendtel: plugin: python python-version: python3 source: .
还有这个setup.py
from setuptools import setup, find_packages import sys,os setup( name = 'send-telemetry-sample', version = '1.0.0', description = 'Send telemetry data to Azure IoT Hub', license='GPL v3', author = 'Eltjon Sulaj', packages = find_packages(), install_requires=['azure-iot-device'], entry_points = { 'console_scripts': [ 'send-telemetry-sample.sendtel=src.SimulatedDevice:main'] }, classifiers = [ 'Programming Language :: Python :: 3.8', 'Operating System :: OS Independent', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)'] )
所有这些都在 Hyper-V 中的 Ubuntu 20.04 虚拟机中运行。
有人对 IoT 快照有一些经验吗?
已解决! 出了问题的是,除非在 snapcraft.yaml 的应用程序元数据中添加了一个接口,否则具有 'strict' 限制的快照无法访问网络资源。 此页面中的更多信息: https://snapcraft.io/docs/snapcraft-interfaces