我有这段代码可以将多个消息发布到不同的主题。
"""
Publish some messages to queue
"""
import paho.mqtt.publish as publish
host ="test.mosquitto.org"
msgs = [{'topic': "A/A1", 'payload': "30"},
{'topic': "A/A2", 'payload': "40"},
{'topic': "A/A3", 'payload': "1"}]
if __name__ == '__main__':
# publish multiple messages
publish.multiple(msgs, hostname=host)
但有时它对我有用并发布数据,有时会引发此错误。
Traceback (most recent call last):
File "/home/pi/Desktop/NRF24L01/publish.py", line 22, in <module>
publish.multiple(msgs, hostname=host)
File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/publish.py", line 159, in multiple
client.connect(hostname, port, keepalive)
File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 839, in connect
return self.reconnect()
File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 962, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
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 -3] **Temporary failure in name resolution**
经过大量研究,我看到一些评论说这是DNS问题或代理已关闭,但无论如何都无法解决。请谁能帮助我? 预先感谢。
答案 0 :(得分:3)
错误的重要部分在这里:
import urlparse
import scrapy
from scrapy.http import Request
class pwc_tax(scrapy.Spider):
name = "sec_gov"
allowed_domains = ["www.sec.gov"]
start_urls = ["https://secsearch.sec.gov/search?utf8=%3F&affiliate=secsearch&query=exhibit+10"]
def parse(self, response):
for href in response.css('div#all_results h3 a::attr(href)').extract():
yield Request(
url=response.urljoin(href),
callback=self.parse_article
)
def parse_article(self, response):
for href in response.css('div.download_wrapper a[href$=".pdf"]::attr(href)').extract():
yield Request(
url=response.urljoin(href),
callback=self.save_pdf
)
def save_pdf(self, response):
path = response.url.split('/')[-1]
self.logger.info('Saving PDF %s', path)
with open(path, 'wb') as f:
f.write(response.body)
这意味着您在其中运行代码的Raspberry Pi找不到socket.gaierror: [Errno -3] **Temporary failure in name resolution**
的IP地址。
这很可能意味着您的代码没有错,但是您需要查看Raspberry Pi如何连接到互联网。
可能有许多问题,但其中一些可能是
不幸的是,我们几乎无法通过任何方式远程了解它。