title: python paho mqtt can not connect to mqtts with username and password
body: <p>The following python code on my raspberrypi doesn't connect to my mqtt broker, it just hangs after printing <code>Connecting...</code>:</p>
  
  <pre><code>import paho.mqtt.client as mqtt
  
  def on_connect(client, userdata, flags, rc):
      print("Connected with result code "+str(rc))
      client.subscribe("test")
  
  
  def on_message(client, userdata, msg):
      print(msg.topic+" "+str(msg.payload))
  
  
  client = mqtt.Client(client_id="",clean_session=True,userdata=None,protocol=mqtt.MQTTv311,transport="tcp")
  client.on_connect = on_connect
  client.on_message = on_message
  
  client.username_pw_set(username="stackoverflow",password="stackoverflow")
  print("Connecting...")
  client.connect("learn.evermight.net", 9101, 10)
  client.loop_forever()
  </code></pre>
  
  <p>What did I do wrong with my Python code?</p>
  
  <hr>
  
  <p><strong>Command Line Success</strong></p>
  
  <p>I confirmed that my mqtt does work because I can subscribe from terminal with this command:</p>
  
  <pre><code>mosquitto_sub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" --capath /etc/ssl/certs/
  </code></pre>
  
  <p>And I will see messages in my terminal as soon as I run this command from another terminal</p>
  
  <pre><code>mosquitto_pub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" -m "hello world" --capath /etc/ssl/certs/
  </code></pre>
  
  <p>What is wrong with my python code?</p>
  
  <hr>
  
  <p><strong>NodeJS Success</strong></p>
  
  <p>Additionally, the following NodeJS code also works for connecting and publishing to my MQTT server.</p>
  
  <pre><code>const mqtt = require('async-mqtt');
  
    try{
      const client = await mqtt.connectAsync("mqtts://learn.evermight.net",{
        port:9101,
        host:"mqtts://learn.evermight.net",
        username:"stackoverflow",
        password:"stackoverflow",
        connectTimeout:5000,
        protocolId:"MQIsdp",
        protocolVersion:3,
        encoding:"utf8",
        keepalive: 60
      });
      await client.publish("test","hello world");
      await client.end();
    } catch(e) {
      console.log(e);
    }
  </code></pre>
  
  <hr>
  
  <p><strong>Website JavaScript Success</strong></p>
  
  <p>And the following code can also connect to a websocket port via web browser javascript website, subscribe to the <code>test</code> topic and receive published messages (note that my websockets use port 9102)</p>
  
  <pre><code>import Paho from "paho-mqtt";
  
  const client = new Paho.Client("learn.evermight.net",9102,"WebBrowser");
    client.onConnectionLost = response=&gt;console.log("lostMQTTConnection: " +(response.errorCode !== 0 ? response.errorMessage : "Unknown MQTT Error" ));
    client.onMessageArrived = message=&gt;console.log(message.payloadString);
    client.connect({
      onSuccess:_=&gt;client.subscribe("test"),
      useSSL:true,
      userName:"stackoverflow",
      password:"stackoverflow",
    });
  </code></pre>