4
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Client paho-mqtt MqttServer
# main.py
import paho.mqtt.publish as publish
from json import dumps
from ssl import PROTOCOL_TLSv1
import urllib2

class MqttClient():
    host = 'mqtt.xyz.com'
    port = '1883'
    auth = {}
    topic = '%s/streams'
    tls = None

    def __init__(self, auth, tls=None):
        self.auth = auth
        self.topic = '%s/streams' % auth['username']
        if tls:
            self.tls = tls
            self.port = '8883'

    def publish(self, msg):
        try:
            publish.single  (topic=self.topic,payload=msg,hostname=self.host,  
                         tls=self.tls,port=self.port)             
        except Exception, ex:
            print ex


if __name__ == '__main__':
    auth = {'username': 'your username', 'password': ''}

    #tls_dict = {'ca_certs': 'ca_certs.crt', 'tls_version': PROTOCOL_TLSv1} # sslvers. 


    msg_dict={'protocol':'v2','device':'Development Device','at':'now','data':{'temp':21,'hum':58}}

    client_mqtt =MqttClient(auth=auth)                       # non ssl version
    #client_mqtt =MqttClient(auth=auth, tls=tls_dict)        # ssl version
    client_mqtt.publish(dumps(msg_dict))

I guess my organization's proxy settings are blocking the request, so please guide me in including the proxy settings in the above code.
For instance if address is 'proxy.xyz.com' and port number is '0000' and my network username is 'xyz' and password is 'abc'

4 Answers4

4

You haven't mentioned what sort of proxy you are talking about, but assuming you want to use a HTTP proxy.

You can not use a HTTP proxy to forward raw MQTT traffic as the two protocols are not compatible.

If the broker you want to connect to supports MQTT over Websockets then you should be able connect vai a modern HTTP proxy, but this will not work with the code you have posted.

As noted in the comments, The Python Paho client added SOCKS and HTTP proxy support under pull request 315.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • So ,according to you, which proxy is suitable for forwarding raw MQTT traffic to open network – saurabh sakharkar Jan 08 '15 at 12:38
  • 1
    You could use a SOCKS proxy and something similar to what is in the first 4 lines of the answer to this question: http://stackoverflow.com/questions/2317849/how-can-i-use-a-socks-4-5-proxy-with-urllib2 – hardillb Jan 08 '15 at 12:55
  • Maybe you can update your answer since it is on the top. Paho has this feature since 2018 July. https://github.com/eclipse/paho.mqtt.python/pull/315 – Ferhat S. R. Jun 09 '20 at 12:51
4

Paho community added this feature on 2018. https://github.com/eclipse/paho.mqtt.python/pull/315

I am adding the code snippet since no one still added it. Just set proxy options for the client object.

client = mqtt.Client()    
client.proxy_set(proxy_type=socks.HTTP, proxy_addr=proxy_host, proxy_port=proxy_port)
Ferhat S. R.
  • 1,129
  • 10
  • 20
2

Your code should something as follow

import socks
import socket
import paho.mqtt.client as mqtt

client = mqtt.Client(client_id='the-client-id')
client.username_pw_set('username', 'password')
# set proxy ONLY after client build but after connect
socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_HTTP, addr="proxy.xyz.com", port=0000, rdns=True,username="xyz", password="abc")
socket.socket = socks.socksocket
# connect
client.connect('mqtt.host', port=1883, keepalive=60, bind_address="")
client.loop_forever()
WILL
  • 21
  • 1
0

I solved it while using a socks proxy and using PySocks

import socks
import socket
import paho.mqtt.client as mqtt

client = mqtt.Client(client_id='the-client-id')
client.username_pw_set('username', 'password')
# set proxy ONLY after client build but after connect
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, "socks.proxy.host", 1080)
socket.socket = socks.socksocket
# connect
client.connect('mqtt.host', port=1883, keepalive=60, bind_address="")
client.loop_forever()
Kier GRAY
  • 143
  • 2
  • 7
  • 1
    This solution is not perfect as it also affects other protocols such as `http`. A merge request has been made to support proxy: https://github.com/eclipse/paho.mqtt.python/pull/315 – Kier GRAY Aug 02 '19 at 13:00