Reputation: 33
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?
Upvotes: 1
Views: 9096
Reputation: 21
Change your topic from "harismuha123@github/#" to "harismuha123@github/whatever" and delete all the comments because the sign "#" is an special character for the mqtt protocol.
Upvotes: 0
Reputation: 465
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.
Upvotes: 2