将MQTT数据包含在SSL证书中,同时将其发送给MQTT代理

时间:2014-07-08 17:09:57

标签: python ssl ssl-certificate raspberry-pi mqtt

我正在使用在RPI上运行的MQTT python客户端。我不是来自Web相关领域的人,但我需要实现SSL安全性,同时我将一些数据从我的python客户端发送到开源MQTT代理。

我在python中找到了certain package,用于在打开套接字时包装SSL安全性。我是python中的新手。所以我想了解它是如何工作的以及如果我们想要实现SSL安全性我们需要做什么。This question解释了很多关于SSl及其如何发生的事情。但是如果我需要用python实现它怎么办?我将如何在我的RPI上本地安装SSL证书(我想要一些开源SSL证书,因为我现在这样做本地项目。)

我使用下面的python代码打开SSL套接字,然后通过443端口连接到www.google.com。

import socket
import ssl

s_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl.wrap_socket(s_, ca_certs='/usr/local/lib/python2.7/dist-packages/requests/cacert.pem',cert_reqs=ssl.CERT_REQUIRED)
s.connect(('www.google.com', 443))
s.write("""GET / HTTP/1.1\r
Host: www.google.com\r\n\r\n""")
d=s.read()
print(d)
s.close()

我在我的控制台上得到了这个输出

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: https://www.google.co.in/?gfe_rd=cr&ei=PkW8U8SsPOqK8Qfwt4DYAw
Content-Length: 262
Date: Tue, 08 Jul 2014 19:23:42 GMT
Server: GFE/2.0
Alternate-Protocol: 443:quic

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.co.in/?gfe_rd=cr&amp;ei=PkW8U8SsPOqK8Qfwt4DYAw">here</A>.
</BODY></HTML>

但我仍然想要或理解(我从可用资源中学习的东西)谁打开SSL套接字需要有SSL证书,我们在那里将SSL证书发送到服务器或这是由openssl库完成的。另外我想确认openssl提供openssl正在使用并发送到服务器的SSL证书吗?

This link帮助了解SSL安全性的基础知识。

1 个答案:

答案 0 :(得分:4)

您是否可以使用Paho Python client库来处理问题的MQTT和SSL?

通过SSL支持在test.mosquitto.org测试服务器上订阅主题并打印收到的消息的一个简单示例:

import paho.mqtt.client as paho

def on_message(clnt, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

mqttc = paho.Client()
mqttc.on_message = on_message
mqttc.tls_set("mosquitto.org.crt") # http://test.mosquitto.org/ssl/mosquitto.org.crt
mqttc.connect("test.mosquitto.org", 8883)
mqttc.subscribe("bbc/#")
mqttc.loop_forever()