通过Python进行MQTT发布:[Errno 101]无法访问网络

时间:2019-10-04 09:10:51

标签: python mqtt paho

我已使用以下脚本在本地网络中的MQTT代理上测试了脚本:

import paho.mqtt.publish as publish

publish.single('some_topic, 'some message', hostname="192.168.1.123")

,一切正常。现在,我想发布到已获得凭据的实际服务器上。可悲的是,所有paho MQTT脚本示例都已发布到本地服务器,因此我无法真正对脚本建模。我提供的凭据包括用户名,密码和主题。作为“主机名”,我进入了公司服务器的网站。

脚本现在显示

import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt

client = mqtt.Client()

client.username_pw_set("given_username",password="given_password")

publish.single("some_topic",'some message',hostname="example.com")

可悲的是,我得到一个错误。

  File "get_datas.py", line 18, in handle_data
    publish.single("some_topic",msg,hostname="example.com")
  File "/usr/local/lib/python3.6/site-packages/paho/mqtt/publish.py", line 223, in single
    protocol, transport)
  File "/usr/local/lib/python3.6/site-packages/paho/mqtt/publish.py", line 159, in multiple
    client.connect(hostname, port, keepalive)
  File "/usr/local/lib/python3.6/site-packages/paho/mqtt/client.py", line 839, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.6/site-packages/paho/mqtt/client.py", line 962, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/local/lib/python3.6/socket.py", line 722, in create_connection
    raise err
  File "/usr/local/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
OSError: [Errno 101] Network is unreachable

我把这个脚本写错了吗?

编辑: 似乎主机名有问题。我可以通过插入服务器的IP地址来解决此问题,该地址是通过

获得的
[11:26:39] $ ping example.com
PING example.com (xx.xxx.xxx.xxx) 56(84) bytes of data.
64 bytes from example.com (xx.xxx.xxx.xxx): icmp_seq=1 ttl=53 time=43.3 ms

2 个答案:

答案 0 :(得分:1)

  1. 您可能缺少在网址中提及协议部分,例如tcp://website.io。对于 websocket tcp ssl 应该适当提及。
  2. 还要检查防火墙中的出站流量规则。 sudo ufw status添加您允许到任何地方的通信协议。

答案 1 :(得分:0)

我测试了与website.io:1883(默认的MQTT端口)的连接

PS C:\> Test-NetConnection website.io -port 1883                                                                        WARNING: TCP connect to (3.220.104.187 : 1883) failed                                                                   WARNING: Ping to 3.220.104.187 failed with status: TimedOut                                                             

ComputerName           : website.io
RemoteAddress          : 3.220.104.187
RemotePort             : 1883
InterfaceAlias         : Ethernet
SourceAddress          : 192.168.123.165
PingSucceeded          : False
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded       : False

TCP ping测试失败。那么您确定地址和端口正确吗?

具有SSL的MQTT(默认MQTT SSL端口为8883)也不会给出TCP响应:

PS C:\> Test-NetConnection website.io -port 8883
WARNING: TCP connect to (3.220.104.187 : 8883) failed
WARNING: Ping to 3.220.104.187 failed with status: TimedOut


ComputerName           : website.io
RemoteAddress          : 3.220.104.187
RemotePort             : 8883
InterfaceAlias         : Ethernet
SourceAddress          : 192.168.123.165
PingSucceeded          : False
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded       : False

https://www.hivemq.com/public-mqtt-broker/是公共MQTT服务器。那是可ping通的:

PS C:\> Test-NetConnection broker.hivemq.com -port 1883                                                                                                                                                                                                                                                                                                                 ComputerName     : broker.hivemq.com
RemoteAddress    : 52.29.71.7
RemotePort       : 1883
InterfaceAlias   : Ethernet
SourceAddress    : 192.168.123.165
TcpTestSucceeded : True