Flask-SocketIO: Socket.io not working on my machine
I’m new in developing and Flask I had 2 projects during my course , the error occurs in the second one only and only when socket.io exist , it’s an empty project but Flask behaves very weird.
for example, every time I run Flask without socket IO it reads something like this
D:\WORK\study sc5\project1\project1>flask run
Serving Flask app "hello"
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
in project 2 we had to work with socket.io and I know it’s the reason because I deleted its line and the weird behavior were gone , also no errors when I ran the socket IO application I get
D:\WORK\study sc5\project2\project2>flask run
* Serving Flask-SocketIO app "application"
that is it after a while I get
[WinError 10053] An established connection was aborted by the software in your host machine
Sun Oct 7 23:47:57 2018 {'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '56449', 'HTTP_HOST': '127.0.0.1:5000', (hidden keys: 23)} failed with ConnectionAbortedError
with that said here is my simple python code
import os, passlib ,requests ,time
from flask import Flask, session , render_template , request,redirect,url_for
from flask_socketio import SocketIO, emit ,join_room, leave_room , Namespace
from datetime import date , datetime
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from passlib.hash import sha256_crypt
engine = create_engine("postgres:**************************",echo=True)
db = scoped_session(sessionmaker(bind=engine))
app = Flask(__name__)
app.config["SECRET_KEY"] = 'secrettt'
#sess.init_app(app)
socketio = SocketIO(app)
class NRoom:
def __init__(self,Rowner,Rname,Rpw):
self.Rowner = Rowner
self.Rname = Rname
self.Rpw = Rpw
class MyCustomNamespace(Namespace):
def on_connect(self):
pass
def on_disconnect(self):
pass
def on_my_event(self, data):
emit('my_response', data)
socketio.on_namespace(MyCustomNamespace('/test'))
@socketio.on('message')
def handle(msg):
print(f'\n\n\n Message:{msg}\n\n\n' )
send(msg, broadcast=True)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/guest" , methods = ["GET" , "POST"])
def guest():
if request.method == "POST":
nickname = request.form.get("guestname")
session["user"] = nickname
print(f"\n\n\n {nickname} \n\n\n\n ")
return redirect(url_for('guest',guestname=nickname))
else:
return render_template("guest.html")
index html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>welcome bk</title>
</head>
<body>
<script type="text/javascript" src="{{url_for('static', filename='javascript.js')}}"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
<script type="text/javascript" charset="utf-8">
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
});
</script>
<script>
document.addEventListener('DOMCOntentLoaded',function(){
var socket = io.connect('http://127.0.0.1:5000');
socket.on('connect',function() {
socket.send("test socketio have passed ! ");
});
});
</script>
<form action="{{ url_for('login') }}">
<input type="submit" value="login" />
</form>
<form action="{{ url_for('regist') }}">
<input type="submit" value="regist" />
</form>
<form action="{{ url_for('guest') }}">
<input type="submit" value="login as guest" />
</form>
</body>
</html>
tested it with chrome and Firefox
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 43 (15 by maintainers)
iam so tired of trying just to get it to work :S
i have installed Cygwin and thatis what happen when i run project 1 which have no Socket io
D:\Work\study\project1>flask run
when i ran project 2 now it shows an error ( which socketIO ):
D:\Work\study\project2org\project2>flask run
then maybe logging for python or flask can provide us with a clue of where the smoke is coming from ?
it would help if there is a way to read loggin from my machine about socket io
i’m sorry as i’ve mentioned earlier i’m googleing and trying different thing just to help us figure it out so i tried to remove socket io and eventlet and reinstall them but i ran into a problem i thought it would share it with you maybe it happen because it might be a clue that something is conflicting with socketIo but i managed to resolve them , thanks to u and stackoverflow
now i tried to run the code after the fresh installation i got the same result
dude literally that is all i have in the code which everyone in my course have it as well but no one with this problem :S
project2.zip
when it run with cgywin >>>>
when i try it with CMD >>>>>
it say server is running on 127.0.0.1:5000 if the code doesnt have socket io in it , if it does it only says what i have mentioned above and yes when i visit 127.0.0.1:5000 i see it working but abslout silence if i tried to send anything from socket io
i had a different version as well , no errors at all , it prints anything related to python but none from socket io
app-test.zip