gpt4 book ai didi

python - getaddrinfo失败,出现socket.gaierror [11001](python)(mqtt)

转载 作者:行者123 更新时间:2023-12-03 11:57:52 30 4
gpt4 key购买 nike

我创建了一个通过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)

我究竟做错了什么?

最佳答案

看来您无法解析主机名。

>>> 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地址,则可以先尝试使用该IP地址。

关于python - getaddrinfo失败,出现socket.gaierror [11001](python)(mqtt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40666194/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com