7

I'm currently trying to understand how sockets work. I'm using Flask-socketio and a python socketio client and running through a basic example. Here is what I have done so far

app.py

from flask import Flask, render_template from flask_socketio import SocketIO, emit app = Flask(__name__) app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app) @socketio.on('aaa') def test_connect(): print("Welcome, aaa received") emit('aaa_response', {'data': 'Server'}) if __name__ == '__main__': socketio.run(app, port=8000) 

client.py

from socketIO_client import SocketIO, LoggingNamespace def on_aaa_response(args): print('on_aaa_response', args['data']) socketIO = SocketIO('localhost', 8000, LoggingNamespace) socketIO.on('aaa_response', on_aaa_response) socketIO.emit('aaa') socketIO.wait(seconds=1) 

I get an assertion error when I run the client.py. I do see the server printing "Welcome, aaa recived" though. I don't know what am I doing wrong here, If thats required here is my log

Error log

Exception in thread Thread-1: Traceback (most recent call last): File "c:\users\dj\appdata\local\programs\python\python36\Lib\threading.py", li ne 916, in _bootstrap_inner self.run() File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\heartbeats.py", line 27, in run self._send_heartbeat() File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\__init__.py", line 203, in _ping engineIO_packet_type, engineIO_packet_data) File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\transports.py", line 109, in send_packet assert response.content == b'ok' AssertionError Traceback (most recent call last): File "demo.py", line 8, in <module> socketIO.emit('aaa') File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\__init__.py", line 424, in emit self._message(str(socketIO_packet_type) + socketIO_packet_data) File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\__init__.py", line 33, in wrap return f(*args, **kw) File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\__init__.py", line 219, in _message transport.send_packet(engineIO_packet_type, engineIO_packet_data) File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\transports.py", line 109, in send_packet assert response.content == b'ok' AssertionError Exception ignored in: <bound method SocketIO.__del__ of <socketIO_client.SocketI O object at 0x00000028079DC320>> Traceback (most recent call last): File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\__init__.py", line 364, in __del__ File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\__init__.py", line 400, in disconnect File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\__init__.py", line 193, in _close File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\transports.py", line 108, in send_packet File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c lient\transports.py", line 191, in get_response File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\s essions.py", line 555, in post File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\s essions.py", line 494, in request File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\s essions.py", line 419, in prepare_request File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\c ookies.py", line 537, in merge_cookies File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\c ookies.py", line 353, in update File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\copy.py", line 96, in co py ImportError: sys.meta_path is None, Python is likely shutting down 
4
  • the stack trace that you copied is missing some stuff on the right side, can you include a complete stack trace please? Also, does anything change if you change the seconds argument to socketIO.wait() to, say, 5 seconds instead of just 1? Commented Jan 9, 2018 at 6:48
  • Thank you for responding @Miguel , I have updated the error Log. And changing socketIO.wait(seconds=5) gives the exact same error. Commented Jan 9, 2018 at 13:15
  • you can check the connection with GUI client firecamp.app Commented Dec 18, 2018 at 18:22
  • i am trying to use something similar. is there a way for the client to timeout if the server is not running(flask_socketio)(usually the server powers off/disconnects) right now my client just waits for the server, i would want it to return if there is no connection available. Commented Aug 27, 2020 at 16:56

2 Answers 2

4

Based on the stack trace, I could not identify the version of the socketIO-client package that you are using. It does not appear to be a current one.

I have tested your two applications here and they seem to work perfectly fine with version 0.7.2 of the client. I suggest you run pip install --upgrade socketIO-client==0.7.2 and then try again.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It works now. I was actually using this version pypi.python.org/pypi/socketIO-client-2/0.7.4 pip install socketIO-client-2
Just in case any one else has this issue, socketIO-client-2 DOES NOT work, use pypi.python.org/pypi/socketIO-client
3

I would recommend using python-socketio for the client and flask + flask-socketio for the server.

Installation

pip install python-socketio pip install flask pip install flask-socketio 

Server

from flask import Flask from flask_socketio import SocketIO, emit import logging app = Flask(__name__) socketio = SocketIO(app) log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) @socketio.on('message') def message(data): print(data) # {'from': 'client'} emit('response', {'from': 'server'}) if __name__ == '__main__': socketio.run(app, port=8000, debug=True) 

Client

import socketio sio = socketio.Client() sio.connect('http://localhost:8000') sio.emit('message', {'from': 'client'}) @sio.on('response') def response(data): print(data) # {'from': 'server'} sio.disconnect() exit(0) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.