1

I am trying to send data from client where I am using python-socketio as mentioend below

#Client.py
import time
import socketio
sio = socketio.Client(engineio_logger=True)
start_timer = None

# if __name__ == '__main__':
sio.connect('http://127.0.0.1:3000')
sio.wait()
sio.emit('connect', {"Data": "Device_id"})

and trying to on server which is using flask-socketio as mentioned below in code

#Server.py
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO
app = Flask(__name__)
# app.config['SECRET_KEY'] = "Social Distance Secret"
socket_app = SocketIO(app)


@socket_app.on('connect')
def handle_id(data):
    print(data)
    print(request.sid)

if __name__ == '__main__':
    socket_app.run(app, debug=True, host='127.0.0.1', port=3000)

I am able to receive the sid but I am not able to fetch the parameters I have given in client.py while emitting to WebSocket

# Error Server.py
Traceback (most recent call last):
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\engineio\server.py", line 545, in _trigger_event
    return self.handlers[event](*args)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 721, in _handle_eio_connect
    return self._handle_connect(sid, '/')
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 626, in _handle_connect
    self.environ[sid])
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 708, in _trigger_event
    return self.handlers[namespace][event](*args)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_socketio-4.3.1.dev0-py3.6.egg\flask_socketio\__init__.py", line 283, in _handler
    *args)
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_socketio-4.3.1.dev0-py3.6.egg\flask_socketio\__init__.py", line 711, in _handle_event
    ret = handler()
TypeError: handle_id() missing 1 required positional argument: 'data'


# Errors client.py:
Attempting polling connection to http://127.0.0.1:3000/socket.io/?transport=polling&EIO=3
Traceback (most recent call last):
  File "C:/Users/varul.jain/Desktop/people_counter/listenn.py", line 12, in <module>
    sio.connect('http://127.0.0.1:3000')
  File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\client.py", line 279, in connect
    six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
  File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Unexpected status code 401 in server response

any suggestion will be really helpful

1 Answer 1

2

First problem is that you are using a connect event, but this event is reserved. Change the event name to something else:

@socket_app.on('connected')
def handle_id(data):
    print(data)
    print(request.sid)

The second problem is that in your client you are calling wait(), which blocks until the connection ends, so your emit() call will never get to run. Send the emit before the wait instead:

sio.connect('http://127.0.0.1:3000')
sio.emit('connected', {"Data": "Device_id"})
sio.wait()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.