python烧瓶射击不连接
我正在使用Blask-Socketio在Python Web服务器和客户端网页之间发送数据。
服务器代码:
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO,emit
app=Flask(__name__, static_url_path='', static_folder="static/")
socketio=SocketIO(app)
@socketio.on('connect')
def test_connect():
print("socket connected")
if __name__=='__main__':
socketio.run(app)
JavaScript客户端代码:
const socket=io('http://localhost:5000');
socket.on('connect',()=>{
console.log('connected!');
}
现在,当我在计算机上本地运行它时,它可以正常运行。 但是当我在 glitch 并运行它,它给了我一些警告,
Serving Flask app (lazy loading)
Environment: production
WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
Debug mode: on
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Restarting with stat WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
Debugger is active!
Debugger PIN: 327-937-508
因为我的代码不起作用。我能够打开index.html,但控制台没有记录“套接字连接”。另外,我的其他插座没有工作。
经过一些堆栈溢出问题后,我将服务器代码更改为:
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO,emit
from gevent.pywsgi import WSGIServer
app=Flask(__name__, static_url_path='', static_folder="static/")
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
socketio=SocketIO(app)
@socketio.on('connect')
def test_connect():
print("socket connected")
if __name__=='__main__':
socketio.run(app)
这修复了警告,但一切仍然不起作用。
有什么方法可以解决这个问题?
I'm using flask-socketio to send data between a python web server and a client web page.
Server Code :
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO,emit
app=Flask(__name__, static_url_path='', static_folder="static/")
socketio=SocketIO(app)
@socketio.on('connect')
def test_connect():
print("socket connected")
if __name__=='__main__':
socketio.run(app)
Javascript client code :
const socket=io('http://localhost:5000');
socket.on('connect',()=>{
console.log('connected!');
}
Now,when I run it locally on my computer,it works fine.
But when I deployed it on Glitch and ran it, It gave me few warnings as :
Serving Flask app (lazy loading)
Environment: production
WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
Debug mode: on
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Restarting with stat WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
Debugger is active!
Debugger PIN: 327-937-508
And my code wasn't working. I was able to open index.html but the console didn't log "socket connected". Also, none of my other socket emits were working.
After going through a few stack overflow questions, I changed my server code to :
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO,emit
from gevent.pywsgi import WSGIServer
app=Flask(__name__, static_url_path='', static_folder="static/")
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
socketio=SocketIO(app)
@socketio.on('connect')
def test_connect():
print("socket connected")
if __name__=='__main__':
socketio.run(app)
This fixed the warnings but everything still doesn't work.
Any way to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。因此,我解决
问题的问题是,客户端是
io('http:// localhost:5000')
,因为客户端不再在本地运行。我通过将客户端更改为
io()
和服务器来修复它Okay. So I got it working
The problem was with client side being
io('http://localhost:5000')
, since the client is no longer running locally.I fixed it by changing client to
io()
and server tosocketio.run(app,port=3000)