我创建了一个通过MQTT发布消息的Android应用程序。我正在创建一个python程序来接收命令。但是,当我运行它时,我总是遇到错误。
Traceback (most recent call last):
File "mqttapptest.py", line 13, in <module>
client.connect(MQTTHOST)
File "E:\Anaconda\lib\site-packages\paho\mqtt\client.py", line 686, in connect
return self.reconnect()
File "E:\Anaconda\lib\site-packages\paho\mqtt\client.py", line 808, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "E:\Anaconda\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "E:\Anaconda\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
我的源代码如下所示:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
MQTTHOST = "free.mqtt.shiguredo.jp"
USERNAME = "<username>"
PASSWORD = "<password>"
client = mqtt.Client(protocol=mqtt.MQTTv311)
client.username_pw_set(USERNAME, PASSWORD)
client.connect(MQTTHOST)
TOPIC = "harismuha123@github/#"
client.subscribe(TOPIC)
client.loop_forever()
TOPIC = "harismuha123@github"
client.publish(TOPIC, "message from python")
import time
time.sleep(0.05)
我做错了什么?
答案 0 :(得分:1)
看起来无法解析主机名。
>>> socket.getaddrinfo('reddit.com', 22)
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('151.101.65.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('151.101.65.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('151.101.65.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('151.101.129.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('151.101.129.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('151.101.129.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('151.101.193.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('151.101.193.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('151.101.193.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('151.101.1.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('151.101.1.140', 22)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('151.101.1.140', 22))]
>>> socket.getaddrinfo('free.mqtt.shiguredo.jp', 8080)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
>>> socket.gethostbyname('free.mqtt.shiguredo.jp')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
>>> socket.gethostbyname('reddit.com')
'151.101.65.140'
如果您有要连接的服务器的IP地址,可以先尝试使用该地址。
答案 1 :(得分:0)
从&#34; harismuha123 @ github /#&#34;更改主题to&#34; harismuha123 @ github / whatever&#34;并删除所有评论,因为标志&#34;#&#34;是mqtt协议的特殊字符。