title: AWS IoT: Use MQTT on port 443
body: <p>I am trying to setup AWS IoT in Pi on port 443 using Paho MQTT .</p>
  
  <p>As AWS document (<a href="https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html</a>) mentioned that </p>
  
  <blockquote>
    <p>Clients wishing to connect using MQTT with X.509 Client Certificate authentication on port 443 must implement the Application Layer Protocol Negotiation (ALPN) TLS extension and pass x-amzn-mqtt-ca as the ProtocolName in the ProtocolNameList.</p>
  </blockquote>
  
  <p>I actually don't know how to achieve it properly in Paho MQTT (<a href="https://github.com/eclipse/paho.mqtt.python" rel="nofollow noreferrer">https://github.com/eclipse/paho.mqtt.python</a>)</p>
  
  <p>What I tried to do (mqtt_apln.py)</p>
  
  <pre><code>import sys
  import ssl
  import time
  import datetime
  import logging, traceback
  import paho.mqtt.client as mqtt
  
  
  MQTT_TOPIC = "topictest"
  MQTT_MSG = "hello MQTT"
  
  
  IoT_protocol_name = "x-amzn-mqtt-ca"
  aws_iot_endpoint = "xxxxxxx.iot.eu-west-1.amazonaws.com"
  url = "https://{}".format(aws_iot_endpoint)
  
  ca = ".xxxxx/rootCA.pem"
  cert = ".xxxxx/xxxxx-certificate.pem.crt"
  private = ".xxxxx/xxxxxx-private.pem.key"
  
  logger = logging.getLogger()
  logger.setLevel(logging.DEBUG)
  handler = logging.StreamHandler(sys.stdout)
  log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  handler.setFormatter(log_format)
  logger.addHandler(handler)
  
  # Define on connect event function
  # We shall subscribe to our Topic in this function
  def on_connect(mosq, obj, rc):
      mqttc.subscribe(MQTT_TOPIC, 0)
  
  # Define on_message event function. 
  # This function will be invoked every time,
  # a new message arrives for the subscribed topic 
  def on_message(mosq, obj, msg):
          print "Topic: " + str(msg.topic)
          print "QoS: " + str(msg.qos)
          print "Payload: " + str(msg.payload)
  
  def on_subscribe(mosq, obj, mid, granted_qos):
      print("Subscribed to Topic: " +
          MQTT_MSG + " with QoS: " + str(granted_qos))
  
  def ssl_alpn():
      try:
          #debug print opnessl version
          logger.info("open ssl version:{}".format(ssl.OPENSSL_VERSION))
          ssl_context = ssl.create_default_context()
          ssl_context.set_alpn_protocols([IoT_protocol_name])
          ssl_context.load_verify_locations(cafile=ca)
          ssl_context.load_cert_chain(certfile=cert, keyfile=private)
  
          return  ssl_context
      except Exception as e:
          print("exception ssl_alpn()")
          raise e
  
  
  mqttc = mqtt.Client()
  
  # Assign event callbacks
  mqttc.on_message = on_message
  mqttc.on_connect = on_connect
  mqttc.on_subscribe = on_subscribe
  
  ssl_context= ssl_alpn()
  mqttc.tls_set_context(context=ssl_context)
  logger.info("start connect")
  mqttc.connect(aws_iot_endpoint, port=443)
  logger.info("connect success")
  mqttc.loop_start()
  </code></pre>
  
  <p>In Pi, I installed python 2.7.14 and paho-mqtt</p>
  
  <p>But When I run python mqtt_apln.py, it shows error: ImportError: No module named paho.mqtt.client</p>
  
  <p>Any suggestion is appreciated</p>