这是我底部的代码是错误:
这是我的配置文件:
[MQTT]
userMQTT = /
passwdMQTT = /
hostMQTT = broker.hivemq.com
poerMQTT = 1883
上面是我使用公共代理的我的配置文件。现在剩下的代码:
import configparser
from time import localtime, strftime
import json
import paho.mqtt.client as mqtt
config = configparser.ConfigParser()
config.read('/home/pi/bin/py.conf') # Broker connection config.
requestTopic = 'services/timeservice/request/+' # Request comes in
here. Note wildcard.
responseTopic = 'services/timeservice/response/' # Response goes
here. Request ID will be appended later
def onConnect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
def onMessage(client, userdata, message):
requestTopic = message.topic
requestID = requestTopic.split('/')[3] # obtain requestID as last
field from the topic
print("Received a time request on topic " + requestTopic + ".")
lTime = strftime('%H:%M:%S', localtime())
client.publish((responseTopic + requestID), payload=lTime, qos=0,
retain=False)
def onDisconnect(client, userdata, message):
print("Disconnected from the broker.")
# Create MQTT client instance
mqttc = mqtt.Client(client_id='raspberrypi', clean_session=True)
mqttc.on_connect = onConnect
mqttc.on_message = onMessage
mqttc.on_disconnect = onDisconnect
# Connect to the broker
mqttc.username_pw_set(config['MQTT']['userMQTT'], password=config['MQTT']
['passwdMQTT'])
但是在我键入后:
mqttc.connect(config['MQTT']['hostMQTT'], port=int(config['MQTT']
['portMQTT']), keepalive=60, bind_address="")
我收到一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line
768, in connect
return self.reconnect()
File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line
895, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=
(self._bind_address, 0))
File "/usr/lib/python3.5/socket.py", line 694, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
我尝试将地址放入bind_address =“”,但我一直收到相同的错误。我尝试放入bind_address =“ 0.0.0.0”或我的本地地址。
答案 0 :(得分:0)
您的配置文件包含:
poerMQTT = 1883
您的代码正在访问的位置:
portMQTT