socket.gaierror: [Errno -2] - PullRequest
       9

socket.gaierror: [Errno -2]

0 голосов
/ 27 июня 2018

Вот мой код внизу - ОШИБКА:

Это мой конфигурационный файл:

[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" или мой локальный адрес.

1 Ответ

0 голосов
/ 27 июня 2018

Ваш конфигурационный файл содержит:

poerMQTT = 1883

Где ваш код обращается к:

portMQTT
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...