MQTT auf ubuntu System

Gast #6751360
Lesenswert?

Guten Morgen,

auf einem ubuntu System habe ich einen MQTT Broker am Laufen. Nun soll 
via python Skript mit einem MQTT Client auf dem ubuntu System eine 
Nachricht veröffentlicht werden. Sobald die folgende Zeile ausgeführt 
wird
1
 client.connect(BROKER_ADDRESS, 1883, 60)

erscheint die Fehlermeldung:
1
File "PythonSkriptTest.py", line 36, in <module>
2
    client.connect(BROKER_ADDRESS, 1883, 60)
3
  File "/usr/local/lib/python3.8/dist-packages/paho/mqtt/client.py", line 941, in connect
4
    return self.reconnect()
5
  File "/usr/local/lib/python3.8/dist-packages/paho/mqtt/client.py", line 1075, in reconnect
6
    sock = self._create_socket_connection()
7
  File "/usr/local/lib/python3.8/dist-packages/paho/mqtt/client.py", line 3546, in _create_socket_connection
8
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
9
  File "/usr/lib/python3.8/socket.py", line 808, in create_connection
10
    raise err
11
  File "/usr/lib/python3.8/socket.py", line 796, in create_connection
12
    sock.connect(sa)
13
ConnectionRefusedError: [Errno 111] Connection refused

Weiß jemand wo da das Problem liegen könnte?

Auf einem raspberry pi funktioniert dieses Skript.
Gast #6751385
Lesenswert?

Wenn ich die IP Adresse "127.0.0.1" dann wird mein Skript nicht mit 
einer Fehlermeldung abgebrochen. Wenn ich nun von einem anderen Rechner 
subscribe und publish ausführen will, welche IP Adresse müsste ich da 
verwenden? Ist es die Adresse "127.0.0.1"?
Gast #6751391
Lesenswert?

> ConnectionRefusedError: [Errno 111] Connection refused

Dein MQTT-Broker nimmt die Anfrage von
1
client.connect(BROKER_ADDRESS, 1883, 60)

nicht an.

Wie ist BROKER_ADDRESS definiert? Und lauscht dann Dein MQTT-Broker auf 
dieser Adresse?
#6751394
Lesenswert?

Connection Refused -> kein MQTT Broker hat sich auf der angegebenen 
Adresse registiert (IP-Adresse und Port). Läuft der Broker überhaupt ?

Auf dem Raspberry und Ubuntu eventuell laufen unterschiedliche Versionen 
eines MQTT Brokers (z.B mosquitto ?) oder die Konfigurationsdateien sind 
unterschiedlich eingestellt. Die Pakete auf dem Rapsberry sind eventuell 
auch für die häufigsten Anwendungsfälle vorkonfiguriert (auf Ubuntu eher 
nicht).

Bei Mosquitto haben sich zwischen den 1.x und 2.x Versionen die 
Defaultwerte für die Listener geändert. Das muss eventuell manuell 
angepasst werden.
Gast #6751398
Lesenswert?

Linux schrieb:
> Ok eines habe ich vergessen mit zu teilen.
>
> Auf dem pi verwende ich den Aufruf: sudo python PythonSkriptTest.py
> Auf dem ubuntu System: sudo python3 PythonSkriptTest.py

Das sudo sollte man sich in dem Fall dringend abgewöhnen.

Falls MQTT Broker nicht am selber Rechner läuft: Nachsehen auf welcher 
Adresse der MQTT Broker hört. 127.0.0.1 ist die Adresse des "Loopback 
Interfaces", die ist von aussen nicht erreichbar. Standard-Praxis unter 
linux ist, dass solche Server-Prozesse ohne Änderung der Konfiguration 
von aussen nicht erreichbar sind, das hat (auch) Sicherheitsgründe.
(Firma: Tamoggemon Holding k.s.) #6751402
Lesenswert?

Denke es ist die neue Version von Mosquitto. Die lehnt Anfragen, die 
nicht von Localhost kommen, von Haus aus ab.

Du musst die Konfiguration anpassen. Weiss jetzt aber auf die Schnelle 
nicht mehr genau, was. Kann es dir aber iwann raussuchen.
Beitrag #6751403 wurde von einem Moderator gelöscht.
Gast #6751405
Lesenswert?

Johannes S. schrieb:
> Was soll die Python Version mit der Fehlermeldung zu tun haben, die sagt
> was schon geschrieben wurde.

Da der TO uns ja nicht verrät ob er per TCP oder über die sockets.py 
kommunizieren will, darf man unterschiede in der entsprechenden 
sockets.py vermuten.
Ausserdem wissen wir nicht, wie und wo der Broker läuft. In einem Docker 
Container wäre localhost zum Beispiel nicht das lo.

Mit dem derzeitigen Informationen ist eine Hilfestellung schwer zu 
leisten.
Gast #6751419
Lesenswert?

Hallo Tam H., das vermute ich auch, dass die Anfragen die nicht von 
localhost kommen abgelehnt werden.

Abgespeckte Skript:
1
import socket
2
import sys
3
import ipaddress
4
import re
5
import time
6
import paho.mqtt.client as mqtt
7
from random import *
8
from datetime import datetime
9

10
SRC_PORT = 4001
11
DST_PORT = 4000
12

13
BROKER_ADDRESS = raw_input("Enter IP address: ")
14
TOPIC = raw_input("Enter topic: ")
15

16
def on_connect(client, userdata, flags, rc):
17
    print("Connected with result code " + str(rc))
18

19
client = mqtt.Client()
20
client.on_connect = on_connect
21

22
client.connect(BROKER_ADDRESS, 1883, 60)
23

24
client.loop_start()
Gast #6751451
Lesenswert?

Linux schrieb:
> Hallo Tam H., das vermute ich auch, dass die Anfragen die nicht von
> localhost kommen abgelehnt werden.

Ich gehe mittlerweile davon aus, dass Tam H. tatsächlich voll in's 
Schwarze getroffen haben mit seiner Vermutung, was die Ursache des 
Problems ist.
Nämlich, dass der MQTT-Broker "mosquitto" ist, auf dem Raspberry Pi 
selbst läuft - und nur über das loopback-Interface zu erreichen ist, 
weil es eine aktuelle Version von mosquitto >= 2.0.0 ist und Du die 
Default-Konfiguration nicht geändert hast.

Und er hat auch bereits klar gesagt, was Du in diesem Fall tun musst, um 
das Problem zu beheben: Die mosquitto-Konfigurationsdatei eben 
dahingehend ändern, dass mosquitto nicht nur auf dem loopback-Interface 
horcht.

Ich zitiere mal das mosquitto-Changelog:
1
Breaking changes:
2
- When the Mosquitto broker is run without configuring any listeners it will
3
  now bind to the loopback interfaces 127.0.0.1 and/or ::1. This means that
4
  only connections from the local host will be possible.
5

6
  Running the broker as `mosquitto` or `mosquitto -p 1883` will bind to the
7
  loopback interface.
8

9
  Running the broker with a configuration file with no listeners configured
10
  will bind to the loopback interface with port 1883.
11

12
  Running the broker with a listener defined will bind by default to `0.0.0.0`
13
  / `::` and so will be accessible from any interface. It is still possible to
14
  bind to a specific address/interface.
15

16
  If the broker is run as `mosquitto -c mosquitto.conf -p 1884`, and a
17
  listener is defined in the configuration file, then the port defined on the
18
  command line will be IGNORED, and no listener configured for it.
19
- All listeners now default to `allow_anonymous false` unless explicitly set
20
  to true in the configuration file. This means that when configuring a
21
  listener the user must either configure an authentication and access control
22
  method, or set `allow_anonymous true`. When the broker is run without a
23
  configured listener, and so binds to the loopback interface, anonymous
24
  connections are allowed.

Hättest Du von Anfang an ein paar mehr Informationen herausgerückt, 
statt uns in bester Salami-Taktik immer nur minimale unvollständige 
Informations-Häppchen hinzuwerfen, hätte man Dir schneller helfen 
können.
Gast #6751786
Lesenswert?

mosquitto.conf:
1
# Place your local configuration in /etc/mosquitto/conf.d/
2
#
3
# A full description of the configuration file is at
4
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
5

6
persistence true
7
persistence_location /var/lib/mosquitto/
8

9
log_dest file /var/log/mosquitto/mosquitto.log
10

11
include_dir /etc/mosquitto/conf.d

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren