如何在Paho-MQTT中添加代理设置? [英] How to add proxy settings in Paho-MQTT?

查看:642
本文介绍了如何在Paho-MQTT中添加代理设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/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))

我想我组织的代理设置阻止了该请求,因此请指导我将代理设置包括在上述代码中.
例如,如果地址是"proxy.xyz.com",端口号是"0000",我的网络用户名是"xyz",密码是"abc"

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'

推荐答案

您尚未提到您正在谈论的是哪种代理,但假设您要使用HTTP代理.

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

您不能使用HTTP代理转发原始MQTT通信,因为这两个协议不兼容.

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

如果您要连接的代理通过Websockets支持MQTT,那么您应该可以通过现代HTTP代理连接,但这不适用于您发布的代码.

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.

这篇关于如何在Paho-MQTT中添加代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆