我的组织有一个企业FW和一个SOCKS代理,允许绕过它。我需要能够将MQTT与外部服务器一起使用。 我可以在python脚本中使用PySocks(对于猴子补丁),但是当我尝试使用paho.mqtt它会中断。
是否有允许使用SOCKS的MQTT库?还有另一种方法可以解决这个问题吗?
#!/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()
错误:
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
答案 0 :(得分:-1)
您可以尝试
client.loop_start()
代替
client.loop_forever()
并检查。应该可以