简体   繁体   English

带有 SOCKS 代理的 Python paho.mqtt

[英]Python paho.mqtt with SOCKS proxy

My organisation has an enterprise FW and a SOCKS proxy to allow bypassing it.我的组织有一个企业固件和一个允许绕过它的 SOCKS 代理。 I need to be able to use MQTT with an external server.我需要能够将 MQTT 与外部服务器一起使用。 I am able to use PySocks in a python script (to monkey patch) but when I try using paho.mqtt it breaks.我能够在 python 脚本中使用 PySocks(到猴子补丁)但是当我尝试使用 paho.mqtt 时它会中断。

Is there an MQTT lib that allowes using SOCKS?是否有允许使用 SOCKS 的 MQTT 库? Is there another way for to go at it?还有另一种方法吗?

#!/usr/bin/python
import socket
import socks
import paho.mqtt.client as mqtt
socks.set_default_proxy(socks.SOCKS5,"my-proxy",1080)
socket.socket = socks.socksocket

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
client.subscribe("$SYS/#")
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("iot.eclipse.org", 1883, 60)
client.loop_forever()

error:错误:

Traceback (most recent call last):   File "./yo.py", line 20, in <module>
    client = mqtt.Client()   File "/usr/lib/python2.7/site-packages/paho/mqtt/client.py", line 508, in
__init__
    self._sockpairR, self._sockpairW = _socketpair_compat()   File "/usr/lib/python2.7/site-packages/paho/mqtt/client.py", line 277, in
_socketpair_compat
    sock1.connect(("127.0.0.1", port))   File "/usr/lib/python2.7/site-packages/socks.py", line 96, in wrapper
    return function(*args, **kwargs)   File "/usr/lib/python2.7/site-packages/socks.py", line 813, in connect
    negotiate(self, dest_addr, dest_port)   File "/usr/lib/python2.7/site-packages/socks.py", line 477, in
_negotiate_SOCKS5
    CONNECT, dest_addr)   File "/usr/lib/python2.7/site-packages/socks.py", line 552, in
_SOCKS5_request
    raise SOCKS5Error("{0:#04x}: {1}".format(status, error)) socks.SOCKS5Error: 0x01: General SOCKS server failure

I think paho python has socks support now as mentioned in the Github url:我认为 paho python 现在有袜子支持,如 Github url 中所述:

paho python setup.py extras paho python setup.py 附加功能

Please look at proxy_set method in client.py请查看client.py中的 proxy_set 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM