python-socketio can not connect to flask server

Created on 25 Dec 2018  路  12Comments  路  Source: miguelgrinberg/python-socketio

question

Most helpful comment

Tried connecting using python client on windows 10
server:

from aiohttp import web
import socketio
import engineio.asyncio_server

sio = socketio.AsyncServer(async_mode='aiohttp')
app = web.Application()
sio.attach(app)

web.run_app(app, host='0.0.0.0', port=8080)

client code:


import socketio

# standard Python
sio = socketio.Client()
if __name__ == '__main__':
    sio.connect('http://0.0.0.0:8080') 
    sio.wait()

client error:

Traceback (most recent call last):
  File "test_win32_v3.py", line 214, in <module>
    sio.connect('http://0.0.0.0:8080')
  File "C:\Users\zoner\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\site-
packages\socketio\client.py", line 210, in connect
    six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
  File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Connection refused by the server

Checked firewall setting, all rules are set for python.exe to access private/public networks with full access

Also turned off firewall for private/public network. still had same error message

EDIT: The issue is that the client needed to have the IP address of the local network .
e.g.

sio.connect('http://192.168.2.16:8080') 

then it worked.

All 12 comments

Sorry,this is my first time to submit a issue. I try to use python-socketio to build a client and use it to connect flask server, which has installed the flask-socketio.But I found that when I emit the message to the flask server ,the server can not receive the message.I use the example from this project( https://github.com/miguelgrinberg/python-socketio/tree/master/examples/client/threads) to build the client and here is some parts of my server code:

@socketio.on('ping_from_client', namespace='/task_status')
def pong(message):
    emit('pong_from_server',{})

I can't really help much without seeing the code. Are you using the same namespace on both sides? You can enable logging on each side to help you figure out where is the problem.

Hi Miguel, I'm facing a similar issue although only with the async client. The client loses connection after a while when connected to Flask-SocketIO server.

Here's the server code:

from eventlet import monkey_patch
monkey_patch()
from flask import Flask, request
from flask_socketio import SocketIO

app = application = Flask(__name__)
socketio = SocketIO(app, logger=True, engineio_logger=True, async_handlers=True)

connected_users = {}

@socketio.on('connect')
def on_connect(**kwargs):
    print("Client connected %s" % request.sid)

@socketio.on('disconnect')
def on_disconnect(**kwargs):
    print("Client disconnected %s" % request.sid)

@socketio.on('ping_from_client')
def ping():
    socketio.emit('pong_from_server')

@socketio.on_error_default
def error_handler(e):
    print("socket error: %s, %s" % (e, str(request.event)))

if __name__ == '__main__':
    socketio.run(app, port=5000, debug=True)

Async client code(from the latency_client.py example):

import asyncio
import time
import socketio

loop = asyncio.get_event_loop()
sio = socketio.AsyncClient(logger=True, engineio_logger=True)
start_timer = None


async def send_ping():
    global start_timer
    start_timer = time.time()
    await sio.emit('ping_from_client')

@sio.on('connect')
async def on_connect():
    print('connected to server')
    await send_ping()

@sio.on('pong_from_server')
async def on_pong(data):
    global start_timer
    latency = time.time() - start_timer
    print('latency is {0:.2f} ms'.format(latency * 1000))
    await sio.sleep(1)
    await send_ping()

async def start_server():
    await sio.connect('http://localhost:5000')
    await sio.wait()

if __name__ == '__main__':
    loop.run_until_complete(start_server())

Client log data:

Sending packet MESSAGE data 2["ping_from_client"]
Received packet MESSAGE data 2["pong_from_server",null]
Received event "pong_from_server" [/]
Emitting event "ping_from_client" [/]
Sending packet MESSAGE data 2["ping_from_client"]
Received packet UNKNOWN data ll]42["pong_from_server",n
Received unexpected packet of type 69
Sending packet PING data None
Received packet PONG data None

The client retries connection and the same thing happens again. The threaded client is working fine though.

@shaan95 I'll look into it. It appears to be a problem with events that have no arguments like these ping and pongs.

@shaan95 using your code I'm not able to reproduce the problem that you get in your logs. Are you running the server and the client from different virtualenvs, such that maybe you are using different versions of some of the packages? Can you send me the requirements.txt for the two sides?

@miguelgrinberg yes I'm running them in separate virtualenvs.
Here is the pip freeze output for the server:

Click==7.0
dnspython==1.16.0
eventlet==0.24.1
Flask==1.0.2
Flask-SocketIO==3.3.1
greenlet==0.4.15
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.1
monotonic==1.5
python-engineio==3.4.3
python-socketio==3.1.2
six==1.12.0
Werkzeug==0.14.1

And the following for the client:

aiohttp==3.5.4
async-timeout==3.0.1
attrs==18.2.0
chardet==3.0.4
idna==2.8
idna-ssl==1.1.0
multidict==4.5.2
python-engineio==3.4.3
python-socketio==3.1.2
six==1.12.0
typing-extensions==3.7.2
websockets==7.0
yarl==1.3.0

The UNKNOWN data packet is received after around 15-17 ping-pongs.

I have come across the same issue. I noticed when I used the requirements.txt from the examples, then it would work. The issue appears to be linked to eventlet. Any version past 0.21.0 will result in this UNKNOWN data packet.

@shaan95 I left the test running for 5 minutes and it still did not reproduce the error. And @Dracio, I'm using eventlet 0.24.1 and still no issue.

That is confusing because when I run your examples:

example/server/wsgi/latency
example/client/asycnio/latency_client

In the following environment I get the "Received unexpected packet of type 69" after about 40 sec.

aiohttp==3.5.4 async-timeout==3.0.1 attrs==19.1.0 chardet==3.0.4 click==7.0 dnspython==1.16.0 eventlet==0.24.1 flask==1.0.2 greenlet==0.4.15 idna-ssl==1.1.0 ; python_version < '3.7' idna==2.8 itsdangerous==1.1.0 jinja2==2.10 markupsafe==1.1.1 monotonic==1.5 multidict==4.5.2 python-engineio==3.4.3 python-socketio==4.0.0 six==1.12.0 typing-extensions==3.7.2 ; python_version < '3.7' websockets==7.0 werkzeug==0.14.1 yarl==1.3.0

I have run it on Window and Linux with Python 3.6 and 3.7.

I will just work with eventlet 0.21 as a workaround.

Can confirm as @Dracio said, it seems to be working well with eventlet 0.21.0. Still getting the same error on 0.24.1. I have tried running the code above with the requirements I posted above and getting the issue on Windows, Linux and MacOS as well.

Tried connecting using python client on windows 10
server:

from aiohttp import web
import socketio
import engineio.asyncio_server

sio = socketio.AsyncServer(async_mode='aiohttp')
app = web.Application()
sio.attach(app)

web.run_app(app, host='0.0.0.0', port=8080)

client code:


import socketio

# standard Python
sio = socketio.Client()
if __name__ == '__main__':
    sio.connect('http://0.0.0.0:8080') 
    sio.wait()

client error:

Traceback (most recent call last):
  File "test_win32_v3.py", line 214, in <module>
    sio.connect('http://0.0.0.0:8080')
  File "C:\Users\zoner\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\site-
packages\socketio\client.py", line 210, in connect
    six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
  File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Connection refused by the server

Checked firewall setting, all rules are set for python.exe to access private/public networks with full access

Also turned off firewall for private/public network. still had same error message

EDIT: The issue is that the client needed to have the IP address of the local network .
e.g.

sio.connect('http://192.168.2.16:8080') 

then it worked.

Tried connecting using python client on windows 10
server:

from aiohttp import web
import socketio
import engineio.asyncio_server

sio = socketio.AsyncServer(async_mode='aiohttp')
app = web.Application()
sio.attach(app)

web.run_app(app, host='0.0.0.0', port=8080)

client code:


import socketio

# standard Python
sio = socketio.Client()
if __name__ == '__main__':
    sio.connect('http://0.0.0.0:8080') 
    sio.wait()

client error:

Traceback (most recent call last):
  File "test_win32_v3.py", line 214, in <module>
    sio.connect('http://0.0.0.0:8080')
  File "C:\Users\zoner\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\site-
packages\socketio\client.py", line 210, in connect
    six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
  File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Connection refused by the server

Checked firewall setting, all rules are set for python.exe to access private/public networks with full access

Also turned off firewall for private/public network. still had same error message

EDIT: The issue is that the client needed to have the IP address of the local network .
e.g.

sio.connect('http://192.168.2.16:8080') 

then it worked.

Thank you. Using my IP in windows 10 it worked!

Was this page helpful?
0 / 5 - 0 ratings