简体   繁体   中英

getaddrinfo failed with socket.gaierror[11001] (python) (mqtt)

I have created an android app that publishes a message over MQTT. I am in the process of creating a python program to receive the commands. However, when I run it I always encounter an error.

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

My source code looks like this :

 #!/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)

What am I doing wrong?

Looks like you can't resolve the hostname.

>>> 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'

If you have the ip address of the server you want to connect to you might try using that first.

将您的主题从“ harismuha123 @ github /#”更改为“ harismuha123 @ github / whatever”,并删除所有注释,因为符号“#”是mqtt协议的特殊字符。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM