1

I'm trying to connect on a broker with the following script

#!/user/bin/env python

import paho.mqtt.client as mqtt
import random
import requests
import warnings
import LoggingManager

logger = LoggingManager.log_setup()
logger.info("Start Working")

my_client_id = f'python-mqtt-{random.randint(0, 1000)}'
# method = "websockets"
my_endpoint = "wss://psmart-test-api.aegean.gr/ws/"
# port = 443
# keepalive = 60
key = "ea75d54ea85b54ba5cde22ffb31090f9b2cbaeba8ad6eef1be575bfae89f56d3"
url_login = "https://psmart-test-auth.aegean.gr/auth/refresh_session"

warnings.filterwarnings('ignore', message='Unverified HTTPS request')


def myNewlogin(key, url_login):
    loginobj = '''{ "token": "%s"  }''' % (key)
    loginHeaders = {'Content-Type': 'application/json'}
    login_response = requests.request("POST", url_login, data=loginobj, headers=loginHeaders, verify=False)

    table = {}
    table["status_code"] = login_response.status_code

    if (login_response.status_code == 200):
        result = login_response.json()
        mytoken = result["token"]
        table["token"] = mytoken
    else:
        table["token"] = login_response.text
    return (table)


is_connected = False

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    global is_connected
    is_connected = True
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    # client.subscribe("$SYS/#")
    # client.subscribe("org/TERIADE/K4XKF6UT/Temperature")


# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))


def on_disconnect(client, userdata, rc):
    print("Disconnected")


# authenticate to take username
# user = str(myNewlogin(key, url_login))
# print(user)

login = myNewlogin(key, url_login)

if (login["status_code"] == 200):  # if we can login
    # print("mylogin 200")
    logger.info("mylogin 200")
else:
    # print(login)
    logger.info("mylogin" + str(login["status_code"]))


# Create a client instance
client = mqtt.Client(my_client_id, transport='websockets')

# Register callbacks
client.on_connect = on_connect
client.on_message = on_message

# client.tls_set(ca_certs="https://psmart-api.aegean.gr/roots.pem")

# client.tls_set()

# Set userid and password
client.username_pw_set(login["token"])

# Connect
client.connect(my_endpoint, 443, 60)

client.loop_start()

publish_interval = 5
value = 0
while 1 == 1:
    if is_connected == True:
        print('hello')
    else:
        print("still waiting for connection")
    time.sleep(publish_interval)


# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
# client.loop_forever()

# while 1:
    # Publish a message every second
    # time.sleep(1)

and I'm getting the following error

Traceback (most recent call last):
  File "mqtt_client_psmart.py", line 91, in <module>
    client.connect(my_endpoint, 443, 60)
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 941, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1075, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3546, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

I can't find a solution. Any ideas? Thanks in advance

After @hardillb help, I made the following changes:

# Configure network encryption and authentication options. Enables SSL/TLS support.
client.tls_set()

Set userid and password

client.username_pw_set(login["token"])

Set websocket connection options.

client.ws_set_options(path="/ws", headers=None)

Connect

client.connect('psmart-test-api.aegean.gr', 443, 60)

The code is running now... I get still waiting for connection but that another issue....

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
sstavridis
  • 13
  • 5

1 Answers1

0

The client.connect() function takes a hostname, not a URL as it's first argument.

The failure is because the client is trying to resolve wss://psmart-test-api.aegean.gr/ws/ as a hostname.

You should just be passing psmart-test-api.aegean.gr as the first argument of the function.

To change the path section of the connect URL then you will need to use the ws_set_options(self, path="/mqtt", headers=None) See the docs here

hardillb
  • 12,813
  • 1
  • 21
  • 34