MQTT 未连接?
MQTT not connecting?
我目前正在尝试使用 paho.mqtt
库设置一个简单的 MQTT 订阅者...
像这样
import paho.mqtt.client as mqtt
hostname = "mqtt://localhost:1883"
client = mqtt.Client(hostname, True, None, mqtt.MQTTv31)
client.connect(hostname)
client.subscribe("hermes/#")
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
client.on_message=on_message
client.loop_forever()
给我这个错误:
Traceback (most recent call last):
File "bedroom.py", line 5, in <module>
client.connect(hostname)
File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 839, in connect
return self.reconnect()
File "/usr/local/lib/python3.7/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/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
我不明白,为什么连接不上?
我在 java 中有类似的脚本,可以正常使用吗?
来自帕霍pythondocs
connect()
connect(host, port=1883, keepalive=60, bind_address="")
The connect() function connects the client to a broker. This is a
blocking function. It takes the following arguments:
host
the hostname or IP address of the remote broker
port
the network port of the server host to connect to. Defaults to 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you are using tls_set() or tls_set_context(), the port may need
providing manually
...
connect()
函数接受主机名或 IP 地址。你已经通过了 URI。如果您从开头删除 mqtt://
并从 hostname
变量中删除 :1883
它将连接。
我目前正在尝试使用 paho.mqtt
库设置一个简单的 MQTT 订阅者...
像这样
import paho.mqtt.client as mqtt
hostname = "mqtt://localhost:1883"
client = mqtt.Client(hostname, True, None, mqtt.MQTTv31)
client.connect(hostname)
client.subscribe("hermes/#")
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
client.on_message=on_message
client.loop_forever()
给我这个错误:
Traceback (most recent call last):
File "bedroom.py", line 5, in <module>
client.connect(hostname)
File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 839, in connect
return self.reconnect()
File "/usr/local/lib/python3.7/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/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
我不明白,为什么连接不上? 我在 java 中有类似的脚本,可以正常使用吗?
来自帕霍pythondocs
connect()
connect(host, port=1883, keepalive=60, bind_address="")
The connect() function connects the client to a broker. This is a blocking function. It takes the following arguments:
host
the hostname or IP address of the remote broker
port
the network port of the server host to connect to. Defaults to 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you are using tls_set() or tls_set_context(), the port may need providing manually
...
connect()
函数接受主机名或 IP 地址。你已经通过了 URI。如果您从开头删除 mqtt://
并从 hostname
变量中删除 :1883
它将连接。