python flask-socketio server receives message but doesn't trigger event - python

I'm using flask and socketio to build a server-client comms system. A simplified version of it is below, which replicates the issue I'm having. Basically, the client can connect to the server and can send messages and receive acknowledgements back, however the messages don't seem to trigger the handler functions on the server. In the example below, I'm expecting the sever to have 'RECEIVED: {} {}' in the logs, as it should call the handle_message method, but there's nothing, despite it specifically saying it's received the 'test' event. I've tried a few different iterations and versions to try to work out what's happening, but I have no idea what's causing the issue. Any help you can provide would be greatly appreciated! Thanks!
test_simple.py:
import sys
import time
from flask import Flask
from flask_socketio import SocketIO
import socketio as sio
from socketio import Client
app = Flask(__name__)
socket = SocketIO(app, engineio_logger=True, logger=True)
#socket.on('test') ## try decorator as .on_event isn't working
def handle_message(*args, **kwargs):
print('RECEIVED: ', args, kwargs)
app.logger.error(f'RECEIVED: {args}, {kwargs}')
socket.emit('reply', 'hello client')
return 'hello client'
def message_callback(*args, **kwargs):
print('CALLBACK: ', args, kwargs)
def startup_server():
#app = Flask(__name__)
#socket = SocketIO(app, cors_allowed_origins="*",
# engineio_logger=True, logger=True)
#socket.on_event('test', handle_message) # this doesn't work either
#socket.on_event('message', handle_message)
socket.run(app, host='127.0.0.1', port=8001, debug=True,
use_reloader=False)
def startup_client():
socket = Client(logger=True, engineio_logger=True)
socket.connect('http://127.0.0.1:8001')
socket.on('reply', message_callback)
socket.emit('test', data='hello world', callback=message_callback)
time.sleep(5)
#socket.send(data='hello message', callback=message_callback)
#time.sleep(5)
socket.disconnect()
if __name__ == '__main__':
if sys.argv[1] == 'predictor':
startup_client()
else:
startup_server()
Server logs:
python3 test_simple.py aggregator
Server initialized for gevent.
54Pavf_I3leodKsoAAAA: Sending packet OPEN data {'sid': '54Pavf_I3leodKsoAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000}
54Pavf_I3leodKsoAAAA: Received request to upgrade to websocket
54Pavf_I3leodKsoAAAA: Upgrade to websocket successful
54Pavf_I3leodKsoAAAA: Received packet MESSAGE data 0
54Pavf_I3leodKsoAAAA: Sending packet MESSAGE data 0{"sid":"xH33UEQVeWzMXauFAAAB"}
54Pavf_I3leodKsoAAAA: Received packet MESSAGE data 21["test","hello world"]
received event "test" from xH33UEQVeWzMXauFAAAB [/]
### << expecting 'RECEIVED: {} {}' here >> ###
54Pavf_I3leodKsoAAAA: Sending packet MESSAGE data 31["",400]
Client logs:
python3 test_simple.py predictor
Server initialized for gevent.
Attempting polling connection to http://127.0.0.1:8001/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': '54Pavf_I3leodKsoAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000}
Engine.IO connection established
Sending packet MESSAGE data 0
Attempting WebSocket upgrade to ws://127.0.0.1:8001/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Received packet NOOP data
Received packet MESSAGE data 0{"sid":"xH33UEQVeWzMXauFAAAB"}
Namespace / is connected
Emitting event "test" [/]
Sending packet MESSAGE data 21["test","hello world"]
Received packet MESSAGE data 31["",400]
Received ack [/]
CALLBACK: ('', 400) {} ## << expected to receive 'hello client' >> ##
## << no 'reply' event trigger >> ##

Solved it - thanks for your help Miguel! The issue was the package versions; I was using Flask 1.1.2 and Flask-SocketIO 4.3.1, which I hadn't realised was a separate package and I'd only tried updating Flask (and socketio-client, but I was already using the most update-to-date version of that). Once I'd updated to Flask 2.0.1 and Flask-SocketIO 5.1.0 as well, it suddenly started working as I'd expected it to. Now I feel dumb, haha

Related

Flask SocketIO web app refuses to accept connections from python file

I am trying to send some data to a Flask app using web sockets. Never done something like this so I might be doing something very wrong but so far I haven't been able to accept a single connection.
For the moment I have 2 python files, server.py and client.py.
server.py starts the flask server and the web socket, then client.py should be able to connect to it, send a message, which is printed out to the server console, then the server should echo that message back where it will be received by the client and print to the client console.
However right now I am getting a Handshake status 400 BAD REQUEST error when the client tries to connect.
Here is the code I'm using:
server.py :
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hi'
socketio = SocketIO(app)
#app.route('/')
def sessions():
return "Hello World"
#socketio.on('message')
def handle_my_custom_event(mes):
print('received my event: ' + str(mes))
socketio.emit('my response', mes)
if __name__ == '__main__':
socketio.run(app, debug=True)
client.py :
import websocket
websocket.enableTrace(True)
ws = websocket.create_connection("ws://localhost:5000")
print("Sending 'Hello, World'...")
ws.send("Hello, World")
print("Sent")
print("Receiving...")
result = ws.recv()
print("Received '%s'" % result)
ws.close()
I think there is something wrong with the server.py file but, i've been flowing the Flask-SocketIO docs and its pretty much identical to their getting started example. But then again, I also don't know enough about this so i have no real idea where the problem lies.
Any help is appreciated thank you!
The problem is with your client. Websocket and socket.io aren't the same, socket.io protocol can use websockets under the hood but you cannot just connect with websocket client to socket.io server.
What you want to use is socket.io client.
And if you don't mind I highly encuorage you to use FastAPI instead of flask. It's much simpler, faster and have much better documentation. Here you can find complete and working example of websocket server and client with FastAPI

How can i invoke a flask endpoint directly from code without using requests

Hey everyone I was tasked a few days ago to create an API style application that listens over a TCP socket for some commands then return some responses mainly success/failures (i know it's dumb but it's the client request) since I have some validation/database stuff I thought of flask directly but I am still stuck on how I am going to invoke the specific endpoints in code directly. here is a small snippet on how I am imagining things would be
from flask import Flask
import threading
data = 'foo'
app = Flask(__name__)
#app.route("/SomeCommand")
def SomeCommand():
return { 'Some' : 'Response'}
def flaskThread():
app.run()
def TcpListenner():
# logic that listens over tcp socket then invoks the flask app
# I was thinking about calling app.something() from here
pass
if __name__ == "__main__":
flaskApp = threading.Thread(target=flaskThread)
flaskApp.start()
listenner = threading.Thread(target=TcpListenner)
listenner.start()
any help/ideas would be much appreciated, thank you
You can use flask_socketio with which the flask app and socket i.e. tcp listener both start together...
Based on what I've understood, you can do something like this:
That the client will first make a connection to the flask socket.
Then, to send a command to the flask app, the client will send a message to the flask socket with the command in its message.
The flask socket will be listening for messages. So when it receives a message for the specific command, then it emits a response based on that command to the socket which will then be received by the client.
Below is an example code for the flask socket app:
import eventlet
eventlet.monkey_patch()
from flask import Flask
from flask_socketio import SocketIO, send, emit
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*", logger=True, engineio_logger=True)
# other flask APIs can come here and can be called by the client...
def someCommandOneResponse(): # function that sends response to client when flask socket gets command 1
commandOneResponse = 'Success'
socketio.emit('message', commandOneResponse)
def someCommandTwoResponse(): # function that sends response to client when flask socket gets command 2
commandTwoResponse = 'Failure'
socketio.emit('message', commandTwoResponse)
#socketio.on('message') # when any command is received on the socket from the client
def handleMessage(cmd):
print('\nCommand Received: ' + cmd + '\n')
if( cmd == 'SomeCommand1' ): # if the client has sent a message for command 1
print('Got Some Command 1')
someCommandOneResponse()
elif( cmd == 'SomeCommand2' ): # if the client has sent a message for command 2
print('Got Some Command 2')
someCommandTwoResponse
send(cmd, broadcast=True)
if __name__ == '__main__':
socketio.run(app, port=3000) # starts the flask socket (tcp listener) as well as the flask app

socket error when connecting paho.mqtt.python to mosquitto broker

I'm trying to connect from python script(paho.mqtt.python) to mosquitto broker.
I can connect from terminal using this command:
mosquitto_sub -h localhost -p 8883 -v -t 'owntracks/#' -u owntracks -P 12qwaszx
But when I'm trying to connect via python script I'm getting the error:
Socket error on client <unknown>, disconnecting.
The script I'm using is the example:
(from here: https://owntracks.org/booklet/tech/program/)
import paho.mqtt.client as mqtt
import json
# The callback for when the client successfully connects to the broker
def on_connect(client, userdata, rc):
''' We subscribe on_connect() so that if we lose the connection
and reconnect, subscriptions will be renewed. '''
client.subscribe("owntracks/+/+")
#tried also: client.subscribe("owntracks/#")
# The callback for when a PUBLISH message is received from the broker
def on_message(client, userdata, msg):
topic = msg.topic
try:
data = json.loads(str(msg.payload))
print "TID = {0} is currently at {1}, {2}".format(data['tid'], data['lat'], data['lon'])
except:
print "Cannot decode data on topic {0}".format(topic)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 8883, 60)
# Blocking call which processes all network traffic and dispatches
# callbacks (see on_*() above). It also handles reconnecting.
client.loop_forever()
Here is the content of my config file (I changed the "localhost" from my real IP - tried both of them):
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 8883 "localhost"
persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfile
Any help would be highly appreciated.
Your python script is attempting to connect to what looks like a TLS secured setup without preparing the client connect method to apply these details to the transaction. Try the following:
def ssl_prep():
ssl_context = ssl.create_default_context()
ssl_context.load_verify_locations(cafile=ca)
ssl_context.load_cert_chain(certfile=mycert, keyfile=priv)
return ssl_context
ca = "PATH_TO_YOUR_CA_FILE"
priv = "PATH_TO_YOUR_PEM_FILE"
mycert = "PATH_TO_YOUR_CERT_FILE"
topics = "YOUR_TOPICS"
broker = "BROKER_URL"
client = mqtt.Client()
ssl_context= ssl_prep()
client.tls_set_context(context=ssl_context)
client.username_pw_set(username="UNAME",password="PASS")
client.connect(broker, port=8883)
By providing the ssl context to the connection attempt before the attempt is made, this should connect assuming you have all the detail specific to your own setup in place.
Try to reduce the KeepAlive time from 1min to 30sec or lower:
client.connect("localhost", 8883, 30)
//Default: connect(host, port=1883, keepalive=60, bind_address=””)

Python, MQTT broker, publish message

I have configured MQTT Broker, receiving published messages from another piece of code (not written or accessible by me). I added another topic to the broker configuration and am now trying to publish data to this new topic from a piece of python code. I get the feedback that the message is published by the callback function, but no actual data is received.
Is there anything I am missing?
I am using the following code:
import paho.mqtt.client as mqtt
import time
#=========================================================================
def on_connect(client, userdata, flags, rc) :
print "on_connect()"
#=========================================================================
def on_publish(client, userdata, mid) :
print "on_publish()"
#=========================================================================
def send() :
mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
#host = "localhost"
host = "127.0.0.1"
port = 1883
keepalive = 60
print "\nConnect to '%s', port '%s', keepalive '%s'" % (host, port, keepalive)
mqttc.connect(host=host, port=port, keepalive=keepalive)
time.sleep(3)
mqttc.loop_start()
time.sleep(3)
topic = "data/MY/TOPIC"
msg = "MY_MESSAGE"
print "Publish to '%s' msg '%s'" % (topic, msg)
mqttc.publish(topic, msg, qos=2)
time.sleep(3)
mqttc.loop_stop()
# end send()
#=========================================================================
if __name__ == "__main__":
send()
# end if
Getting the stdout
Connect to '127.0.0.1', port '1883', keepalive '60'
on_connect()
Publish to 'data/MY/TOPIC' msg 'MY MESSAGE'
on_publish()
I am not sure if the loop() functions are necessary, but if I do not embed the publishing in the loop_start() and loop_stop(), I do not get a on_connect callback.
The loop functions are necessary as these are where all the network traffic is processed.
Manually setting up a connection to the broker to just send a single message like this is not a good idea, it would be better to start the client, leave it running (by calling loop_start() and not calling loop_stop()) in the background and then just call the publish method on the mqttc client object.
If you don't want to keep a instance of the client running then you should use the single message publish helper method provided by the paho python library (https://pypi.python.org/pypi/paho-mqtt/1.1#single):
import paho.mqtt.publish as publish
publish.single("paho/test/single", "payload", hostname="iot.eclipse.org")

Policy Server with gevent-websocket

working on trying to get gevent-websocket working and it's not connecting to my policy server for the flash specification. My policy.py is as follows:
from gevent.server import StreamServer
policy = """<?xml version="1.0" encoding="UTF-8"?>
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="*" to-ports="*" secure="false"/>
</cross-domain-policy>\0"""
def handle(sock, address):
s = sock.makefile()
while True:
msg = s.readline()
if not msg:
print("Client disconnected (%s:%s)" % address)
break
else:
sock.sendall(policy)
print("Client connected, served policy (%s:%s)" % address)
server = StreamServer(('0.0.0.0', 843), handle)
server.serve_forever()
Yet with websocket I'm getting:
[WebSocket] policy file: xmlsocket://localhost:843
[WebSocket] cannot connect to Web Socket server at ws://localhost:8065 (SecurityError: Error #2048) make sure the server is running and Flash socket policy file is correctly placed
[WebSocket] close
the readline() method won't work here, because Flash sends a '<policy-file-request/>\0' which is not terminated by newline.
Try this instead of readline():
expected = '<policy-file-request>'
s.read(len(expected))

Categories