kopia lustrzana https://github.com/micropython/micropython-lib
umqtt.simple: Add SSL support.
rodzic
4a8ef9f99c
commit
208c7f0f17
|
@ -7,10 +7,13 @@ class MQTTException(Exception):
|
|||
|
||||
class MQTTClient:
|
||||
|
||||
def __init__(self, client_id, server, port=1883):
|
||||
def __init__(self, client_id, server, port=0, ssl=False):
|
||||
if port == 0:
|
||||
port = 8883 if ssl else 1883
|
||||
self.client_id = client_id
|
||||
self.sock = None
|
||||
self.addr = socket.getaddrinfo(server, port)[0][-1]
|
||||
self.ssl = ssl
|
||||
self.pid = 0
|
||||
self.cb = None
|
||||
|
||||
|
@ -34,6 +37,9 @@ class MQTTClient:
|
|||
def connect(self, clean_session=True):
|
||||
self.sock = socket.socket()
|
||||
self.sock.connect(self.addr)
|
||||
if self.ssl:
|
||||
import ussl
|
||||
self.sock = ussl.wrap_socket(self.sock)
|
||||
msg = bytearray(b"\x10\0\0\x04MQTT\x04\x02\0\0")
|
||||
msg[1] = 10 + 2 + len(self.client_id)
|
||||
msg[9] = clean_session << 1
|
||||
|
|
Ładowanie…
Reference in New Issue