Trying to send message to IoT Hub using azure sdk but getting 'certfile should be a valid filesystem path' error
See original GitHub issueI am using one of the sample code provided by this git repo azure-iot-sdk-python/azure-iot-device/samples/async-hub-scenarios/send_message_x509.py and trying to connect my Iot Device to IoT Hub but getting an error. I am sending my code and error. Please help
Code
-------------------------------------------------------------------------
Copyright © Microsoft Corporation. All rights reserved.
Licensed under the MIT License. See License.txt in the project root for
license information.
--------------------------------------------------------------------------
import os import uuid from azure.iot.device.aio import IoTHubDeviceClient from azure.iot.device import Message, X509 import asyncio
messages_to_send = 10
async def main(): hostname = os.getenv(“WE-SB-DEV-SC-iothub02.azure-devices.net”) # The device that has been created on the portal using X509 CA signing or Self signing capabilities device_id = os.getenv(“leafrootca”)
x509 = X509(
cert_file=os.getenv("/Users/r0m086g/Documents/DeviceCerti/iot-device-leafrootca-primary.cert.pem"),
key_file=os.getenv("/Users/r0m086g/Documents/DeviceCerti/iot-device-leafrootca-primary.key.pem"),
pass_phrase=os.getenv("1234")
)
# The client object is used to interact with your Azure IoT hub.
device_client = IoTHubDeviceClient.create_from_x509_certificate(
hostname=hostname, device_id=device_id, x509=x509
)
# Connect the client.
await device_client.connect()
async def send_test_message(i):
print("sending message #" + str(i))
msg = Message("test wind speed " + str(i))
msg.message_id = uuid.uuid4()
msg.correlation_id = "correlation-1234"
msg.custom_properties["tornado-warning"] = "yes"
msg.content_encoding = "utf-8"
msg.content_type = "application/json"
await device_client.send_message(msg)
print("done sending message #" + str(i))
# send `messages_to_send` messages in parallel
await asyncio.gather(*[send_test_message(i) for i in range(1, messages_to_send + 1)])
# Finally, shut down the client
await device_client.shutdown()
if name == “main”: asyncio.run(main())
# If using Python 3.6 or below, use the following code instead of asyncio.run(main()):
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())
# loop.close()
Error
Unexpected error in <azure.iot.device.common.pipeline.pipeline_stages_http.HTTPTransportStage object at 0x7fd77de159a0>._run_op() call Traceback (most recent call last): File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/pipeline/pipeline_stages_base.py”, line 103, in run_op self._run_op(op) File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/pipeline/pipeline_thread.py”, line 203, in wrapper return func(*args, **kwargs) File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/pipeline/pipeline_stages_http.py”, line 62, in _run_op self.transport = HTTPTransport( File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/http_transport.py”, line 37, in init self._ssl_context = self._create_ssl_context() File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/http_transport.py”, line 60, in _create_ssl_context ssl_context.load_cert_chain( TypeError: certfile should be a valid filesystem path
Traceback (most recent call last): File “/Users/r0m086g/Desktop/IOT_Platform_Tasks/Leafdevice/leafAmqp.py”, line 55, in <module> asyncio.run(main()) File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/runners.py”, line 43, in run return loop.run_until_complete(main) File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py”, line 616, in run_until_complete return future.result() File “/Users/r0m086g/Desktop/IOT_Platform_Tasks/Leafdevice/leafAmqp.py”, line 29, in main device_client = IoTHubDeviceClient.create_from_x509_certificate( File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/aio/patch_documentation.py”, line 114, in create_from_x509_certificate return super(IoTHubDeviceClient_, cls).create_from_x509_certificate( File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/iothub/abstract_clients.py”, line 498, in create_from_x509_certificate http_pipeline = pipeline.HTTPPipeline(pipeline_configuration) File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/iothub/pipeline/http_pipeline.py”, line 65, in init callback.wait_for_completion() (base) r0m086g@m-c02d14uwmd6n Leafdevice % /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/r0m086g/Desktop/IOT_Platform_Tasks/Leafdevice/leafAmqp.py Unexpected error in <azure.iot.device.common.pipeline.pipeline_stages_http.HTTPTransportStage object at 0x7fea2e1e39a0>._run_op() call Traceback (most recent call last): File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/pipeline/pipeline_stages_base.py”, line 103, in run_op self._run_op(op) File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/pipeline/pipeline_thread.py”, line 203, in wrapper return func(*args, **kwargs) File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/pipeline/pipeline_stages_http.py”, line 62, in _run_op self.transport = HTTPTransport( File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/http_transport.py”, line 37, in init self._ssl_context = self._create_ssl_context() File “/Users/r0m086g/Library/Python/3.8/lib/python/site-packages/azure/iot/device/common/http_transport.py”, line 60, in _create_ssl_context ssl_context.load_cert_chain( TypeError: certfile should be a valid filesystem path
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
@RohitMundhra
os.getenv()
is a python built-in function that fetches a environment variable. You seem to be trying to provide a filesystem path to it instead of the name of a variable where the path is stored.Try just passing the filepath strings directly as the arguments
cert_file
andkey_file
without use of theos.getenv()
functionClosing due to inactivity.