Websocket
Verfasst: Mittwoch 12. April 2023, 07:11
Hallo zusammen ich habe da ein Problem mit dem zugriff auf einen Websocket. Mit dem MQTT Browser kann ich über ws direkt auf den Server zugreifen. jedoch mit python geht es nicht, ich glaube es liegt an der Autehtifikation. Wenn ich das ping pong mache, sehe ich per wireshark, dass ein pong an die adresse gemacht wird, jedoch kommt bei mir None. habe einige variationen getestet, aber nicht geht. Weiss jemand weiter oder ein guter Rat?
# Pakete importieren
import websocket
import ssl
import json
import time
import numpy as np
import csv
import matplotlib.pyplot as plt
# Allgemeine Einstellungen
path = ["C:\\Users\\buehlpas\\Desktop\\Messungen\\"]
name = ["Smart_Sensor_WS"]
ending = [".csv", ".svg"]
datum = time.localtime()
format('%4d')
# Definition Variablen
m = 1 # Anzahl Messdurchlauf
n = 10 # Anzahl Messpunkte
t0 = time.time()
t = []
u = []
v = []
w = []
z = []
header = ["Time t [s]", "MQTT Topic [num]", "MQTT Payload [num]"]
body = []
topics = [("sauter/ecos504/411001065754/status/analog-value/1/present-value", 0),
("sauter/ecos504/411001065754/status/analog-value/2/present-value", 0)]
active = True
# The callback for when the client is connected to the server.
def on_connect(ws):
print("Connection opened")
# The callback for when the client is disconnected to the server.
def on_close():
print("Connection closed")
# The callback for when the client has an error to the server.
def on_error(ws, error):
print(error)
# The callback for when a message is received from the server.
def on_receive(ws, msg):
global t
global active
print("Received message '" + str(msg.payload.decode("utf-8")) + "' on topic '"
+ msg.topic + "' with QoS " + str(msg.qos) + "' Count " + str(len(u)))
t.append(str(time.time() - t0))
# u.append(str(msg.topic).split("/")[5])
# v.append(str(msg.payload.decode("utf-8")))
# body.append([t[-1], u[-1], v[-1]])
if len(t) > n:
active = False
# The callback for when a message is published from the server.
def on_send(ws, payload):
print("Message Sent...")
def on_pong(ws, message):
print("Got a pong! No need to respond")
# Callback Function
websocket.enableTrace(True)
ws = websocket.WebSocket()
# ws.connect = on_connect
ws.close = on_close
ws.error = on_error
# ws.recv = on_receive
# ws.send = on_send
ws.pong = on_pong
# WS Definition
host = "192.168.10.70"
port = "9001"
username = 'admin'
password = 'sauter'
# connect to broker
ws.connect("ws://admin:sauter@192.168.10.70:9001", origin="192.168.10.11", subprotocols=["mqtt"])
print(ws.getheaders())
print(ws.getstatus())
print(ws.getsubprotocol())
print(ws.ping("This is an optional ping payload"))
# ws.send("ws://192.168.10.70:9001/sauter/ecos504/411001065754/status/analog-value/1/present-value", "10.00")
# wsapp = websocket.WebSocketApp("ws://192.168.10.70:9001/sauter/ecos504/411001065754/status/", on_message=on_message)
# Start the loop
# wsapp.run_forever(http_proxy_host=host, http_proxy_port=port, http_proxy_auth=(username, password))
# Smart Sensor datas
# Loop Bedingung
# while active:
# time.sleep(1)
# Bedingung Erfüllt Loop wird gestoppt
# wsapp.on_close
# Messung Ende, Schnittstelle freigeben
ws.close()
# Pakete importieren
import websocket
import ssl
import json
import time
import numpy as np
import csv
import matplotlib.pyplot as plt
# Allgemeine Einstellungen
path = ["C:\\Users\\buehlpas\\Desktop\\Messungen\\"]
name = ["Smart_Sensor_WS"]
ending = [".csv", ".svg"]
datum = time.localtime()
format('%4d')
# Definition Variablen
m = 1 # Anzahl Messdurchlauf
n = 10 # Anzahl Messpunkte
t0 = time.time()
t = []
u = []
v = []
w = []
z = []
header = ["Time t [s]", "MQTT Topic [num]", "MQTT Payload [num]"]
body = []
topics = [("sauter/ecos504/411001065754/status/analog-value/1/present-value", 0),
("sauter/ecos504/411001065754/status/analog-value/2/present-value", 0)]
active = True
# The callback for when the client is connected to the server.
def on_connect(ws):
print("Connection opened")
# The callback for when the client is disconnected to the server.
def on_close():
print("Connection closed")
# The callback for when the client has an error to the server.
def on_error(ws, error):
print(error)
# The callback for when a message is received from the server.
def on_receive(ws, msg):
global t
global active
print("Received message '" + str(msg.payload.decode("utf-8")) + "' on topic '"
+ msg.topic + "' with QoS " + str(msg.qos) + "' Count " + str(len(u)))
t.append(str(time.time() - t0))
# u.append(str(msg.topic).split("/")[5])
# v.append(str(msg.payload.decode("utf-8")))
# body.append([t[-1], u[-1], v[-1]])
if len(t) > n:
active = False
# The callback for when a message is published from the server.
def on_send(ws, payload):
print("Message Sent...")
def on_pong(ws, message):
print("Got a pong! No need to respond")
# Callback Function
websocket.enableTrace(True)
ws = websocket.WebSocket()
# ws.connect = on_connect
ws.close = on_close
ws.error = on_error
# ws.recv = on_receive
# ws.send = on_send
ws.pong = on_pong
# WS Definition
host = "192.168.10.70"
port = "9001"
username = 'admin'
password = 'sauter'
# connect to broker
ws.connect("ws://admin:sauter@192.168.10.70:9001", origin="192.168.10.11", subprotocols=["mqtt"])
print(ws.getheaders())
print(ws.getstatus())
print(ws.getsubprotocol())
print(ws.ping("This is an optional ping payload"))
# ws.send("ws://192.168.10.70:9001/sauter/ecos504/411001065754/status/analog-value/1/present-value", "10.00")
# wsapp = websocket.WebSocketApp("ws://192.168.10.70:9001/sauter/ecos504/411001065754/status/", on_message=on_message)
# Start the loop
# wsapp.run_forever(http_proxy_host=host, http_proxy_port=port, http_proxy_auth=(username, password))
# Smart Sensor datas
# Loop Bedingung
# while active:
# time.sleep(1)
# Bedingung Erfüllt Loop wird gestoppt
# wsapp.on_close
# Messung Ende, Schnittstelle freigeben
ws.close()