From dbb744168e43825a4102d201250eeb9369896e8e Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Wed, 27 Nov 2013 21:51:46 +0100 Subject: [PATCH] Enable loading without ssl import Until now, an `import mosquitto' fails on platforms without ssl. This patch allows client.py to be used in spite of not being able to import ssl; If a client attempts to use the tls_() functions, an error is raised. Signed-off-by: Jan-Piet Mens --- src/paho/mqtt/client.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 13365b1..413f518 100755 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -20,7 +20,15 @@ import errno import random import select import socket -import ssl +HAVE_SSL = True +try: + import ssl + cert_reqs = ssl.CERT_REQUIRED + tls_version = ssl.PROTOCOL_TLSv1 +except: + HAVE_SSL = False + cert_reqs = None + tls_version = None import struct import sys import threading @@ -446,7 +454,7 @@ class Client: self._sock = None self.__init__(client_id, clean_session, userdata) - def tls_set(self, ca_certs, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None): + def tls_set(self, ca_certs, certfile=None, keyfile=None, cert_reqs=cert_reqs, tls_version=tls_version, ciphers=None): """Configure network encryption and authentication options. Enables SSL/TLS support. ca_certs : a string path to the Certificate Authority certificate files @@ -481,6 +489,9 @@ class Client: more information. Must be called before connect() or connect_async().""" + if HAVE_SSL == False: + raise ValueError('This platform has no SSL/TLS.') + if sys.version < '2.7': raise ValueError('Python 2.7 is the minimum supported version for TLS.') @@ -528,6 +539,9 @@ class Client: there is no point using encryption. Must be called before connect().""" + if HAVE_SSL == False: + raise ValueError('This platform has no SSL/TLS.') + self._tls_insecure = value -- 2.39.5