def secure_connection(connection, key_file=None, cert_file=None): """Add a layer of security on top of the supplied connection. Please note that this _may_ only work for client to server connections! """ if key_file or cert_file: print "using key %s and cert %s" % (key_file, cert_file) ssl_connection = socket.ssl(connection, key_file, cert_file) else: print "NOT using key and cert" ssl_connection = socket.ssl(connection) return ssl_connection