question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trying to send message to IoT Hub using azure sdk but getting 'certfile should be a valid filesystem path' error

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cartertinneycommented, Aug 9, 2021

@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 and key_file without use of the os.getenv() function

0reactions
cartertinneycommented, Aug 30, 2021

Closing due to inactivity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Azure IoT Hub error codes | Microsoft Learn
This article describes the causes and solutions for common error codes that you might encounter while using IoT Hub.
Read more >
Issue with sending data to azure IoT HUB - Microsoft Q&A
Please, break down the issue. First try to get confirmation your device is capable to connect to your IoT Hub by running some...
Read more >
Troubleshoot Azure IoT Edge common errors - Microsoft Learn
Use this article to identify and resolve common issues when using IoT Edge solutions. If you need information on how to find logs...
Read more >
IoT Hub API common error codes - Microsoft Learn
Error code Description HTTP status code 400103 InvalidContentEncodingOrType 400 Bad Request 401002 IotHub Unauthorized Access 401 Unauthorized 403002 See 403002 IoTHubQuotaExceeded. 403 Forbidden
Read more >
Troubleshoot device connections to Azure IoT Central
This document helps you find out why the data your devices are sending to IoT Central may not be showing up in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found