用pah通过crt/pem

2024-04-24 17:31:48 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我正试图找出如何为我们的应用程序使用.crt和.pem文件来发布MQTT。

下面是一些我要做的伪代码: 通过tls连接到现有主题 发布消息

import paho.mqtt.publish as mqtt
import paho.mqtt.client as mqttclient  
topic = "Some/Topic/Goes/Here"
my_ca_cert = open("<path_to_cert>\ca.crt", 'rb').read()
my_pri_cert = open("<path_to_cert>\private.pem", 'rb').read()
my_key_cert = open("<path_to_cert>\certificate.pem", 'rb').read()

mqttc = mqttclient.Client("Python_Ex_Pub")
mqttc.tls_set(my_ca_cert, certfile=my_pri_cert, keyfile=my_key_cert)
mqttc.connect("<gateway_address>", 8883)
mqttc.publish(topic_name, "This is a test pub from Python.")
mqttc.loop(timeout=2.0, max_packets=1)

当我运行脚本时,将引发以下错误:

Traceback (most recent call last):
  File "mqtt_pub_test.py", line 9, in <module>
    mqttc.tls_set(my_ca_cert, certfile=my_pri_cert, keyfile=my_key_cert)
  File "C:\Python27\lib\site-packages\paho\mqtt\client.py", line 557, in tls_set

    raise IOError(ca_certs+": "+err.strerror)
IOError: -----BEGIN CERTIFICATE-----
<cert_info_here>
-----END CERTIFICATE-----: No such file or directory

我阅读了paho doc页面上的TLS示例,但不知道如何在代码中传递crt/pem文件。 有一次,我只指向包含文件的文件夹,甚至指向chmod 777文件夹,但在运行时,python拒绝了我的访问。

任何帮助都会得到通知


Tags: 文件topathreadcertmytlsopen