Search code examples
pythonwindowsflaskportflask-socketio

Running a python flask application on windows


I have a python flask application I've been running on linux without issue, but recently it's become a requirement to run it on windows and I've run into a problem. When trying to connect to a server like this:

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
socketio = SocketIO(app=app, logging='DEBUG')

@socketio.on_error_default
def error_handler(e):
    print('An error occurred:', e)

app.route('/')
def index():
    return 'Hello, Socket.IO Client!'

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

with a socketio client like this:

import socketio
import logging

sio = socketio.Client()
logging.basicConfig(level=logging.DEBUG)

@sio.event
def connect():
    print('Connected to server')

@sio.event
def disconnect():
    print('Disconnected from server')

sio.connect('http://localhost:5000')

I get the error "socketio.exceptions.ConnectionError: One or more namespaces failed to connect".

This doesn't happen when running the exact same code on ubuntu. I'm losing my mind rn so any help here would be appreciated beyond words, even if it's just verifying that the error reproduces on your machine.


Python is version 3.9.13 Package versions are as follows:

  • Flask==2.0.1
  • flask-restplus==0.13.0
  • Flask-SocketIO==5.1.0
  • python-engineio==4.2.0

And also, though I'm fairly sure it's not causing any issues here, python-socketio==5.3.0

Additional Info:

  • Tested for windows 10 & 11
  • Curling localhost:5000 returns the message 0{"sid":"vOhTP9YKne00Ixa5AAAC","upgrades":["websocket"],"pingTimeout":20000,"pingInterval":25000} so I'm fairly sure it's an issue with the client
  • Running without the firewall doesn't help
  • Running on a different port doesn't help
  • Running as admin doesn't help
  • Running using flask run instead of python doesn't help
  • Running on a linux docker container running on a windows host machine works with no issues
  • I haven't touched the network settings at all or done any kind of port forwarding

Full client log:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:5000
DEBUG:urllib3.connectionpool:http://localhost:5000 "GET /socket.io/?transport=polling&EIO=4&t=1688031616.8050513 HTTP/1.1" 200 None
websocket-client package not installed, only polling transport is available
ERROR:engineio.client:websocket-client package not installed, only polling transport is available
DEBUG:urllib3.connectionpool:Resetting dropped connection: localhost
DEBUG:urllib3.connectionpool:Starting new HTTP connection (2): localhost:5000
Traceback (most recent call last):
  File "C:\Users\Matt\source\repos\data-art\hg-backend\Client.py", line 15, in <module>
    sio.connect('http://localhost:5000')
  File "C:\Users\Matt\source\repos\data-art\hg-backend\venv\lib\site-packages\socketio\client.py", line 323, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
DEBUG:urllib3.connectionpool:http://localhost:5000 "POST /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:http://localhost:5000 "GET /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA&t=1688031618.8611357 HTTP/1.1" 200 None
Connected to server

Full server log:

 * Serving Flask app 'Server' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [29/Jun/2023 10:40:18] "GET /socket.io/?transport=polling&EIO=4&t=1688031616.8050513 HTTP/1.1" 200 -
127.0.0.1 - - [29/Jun/2023 10:40:20] "POST /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA HTTP/1.1" 200 -
127.0.0.1 - - [29/Jun/2023 10:40:20] "GET /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA&t=1688031618.8611357 HTTP/1.1" 200 -
127.0.0.1 - - [29/Jun/2023 10:41:20] "GET /socket.io/?EIO=4&transport=polling HTTP/1.1" 200 -

Solution

  • Have you tried this?

    sio.connect('http://localhost:5000', wait_timeout = 10)