flask socketio 400(错误请求)
flask socketio 400 (BAD REQUEST)
我正在尝试使用 flask socketio 来检索一些实时推文,目前我正在使用 socketio 看看它是如何工作的。但是在我的测试中,我时不时得到 400 (BAD REQUEST)。我想知道是什么问题。
这是错误示例:
socket.io.js:3511 POST http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYW0D&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)Request.create @ socket.io.js:3511Request @ socket.io.js:3424XHR.request @ socket.io.js:3352XHR.doWrite @ socket.io.js:3365(anonymous function) @ socket.io.js:3884(anonymous function) @ socket.io.js:4747proxy @ socket.io.js:1197(anonymous function) @ socket.io.js:4762(anonymous function) @ socket.io.js:4742exports.encodePacket @ socket.io.js:4541encodeOne @ socket.io.js:4741eachWithIndex @ socket.io.js:4760map @ socket.io.js:4767exports.encodePayload @ socket.io.js:4746Polling.write @ socket.io.js:3883Transport.send @ socket.io.js:2912Socket.flush @ socket.io.js:2623Socket.sendPacket @ socket.io.js:2683Socket.ping @ socket.io.js:2587(anonymous function) @ socket.io.js:2574
socket.io.js:3511 GET http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYPYb&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)Request.create @ socket.io.js:3511Request @ socket.io.js:3424XHR.request @ socket.io.js:3352XHR.doPoll @ socket.io.js:3382Polling.poll @ socket.io.js:3795Polling.onData @ socket.io.js:3834(anonymous function) @ socket.io.js:3385Emitter.emit @ socket.io.js:4389Request.onData @ socket.io.js:3546Request.onLoad @ socket.io.js:3627xhr.onreadystatechange @ socket.io.js:3499
socket.io.js:3511 POST http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYeOF&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)
我的服务器端代码(python flask):
from flask import Flask
from flask_socketio import SocketIO
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret!"
socketio = SocketIO(app)
def root():
return app.send_static_file("index.html")
@socketio.on("connect", namespace="/npTweet")
def connectServer():
print("Client connected")
socketio.emit("connected", namespace="/npTweet")
@socketio.on("startTweets", namespace="/npTweet")
def tweetStreaming():
print("Start streaming tweets...")
socketio.emit("streamTweets", {"stream_result": "test"}, namespace="/npTweet")
if __name__ == "__main__":
socketio.run(app, debug=True)
我的客户端代码:
socket = io.connect("http://" + document.domain + ":" + location.port + "/npTweet");
console.log("http://" + document.domain + ":" + location.port + "/npTweet");
// listen to the event 'connected'
socket.on("connected", function(data){
console.log("listening connected...");
socket.emit("startTweets")
});
socket.on("streamTweets", function(data){
console.log("listen streamTweets...");
console.log(data.stream_result)
});
这些是我启动 index.html 页面时的初始输出:
http://127.0.0.1:5000/npTweet
listening connected...
listen streamTweets...
test
但是大概过了30秒左右,就开始出现之前的400 bad request错误,接着是下面的结果:
listening connected...
listen streamTweets...
test
然后大约 30 秒后,它再次执行错误的请求和结果。我哪里做错了?任何帮助将不胜感激。
对于客户端,我使用的是 socketio js 1.4.5
对于 python:
Flask-SocketIO==2.5
python-engineio==0.9.2
这也作为 GitHub 问题发布 here。
根据新的 Flask 0.11 CLI,问题是 Flask 服务器是作为 flask run
启动的。不幸的是,这种启动服务器的方法绕过了 Socket.IO 代码。解决方案是 运行 脚本,以便 if __name__ == 'main':
块内的代码到达 运行.
我正在尝试使用 flask socketio 来检索一些实时推文,目前我正在使用 socketio 看看它是如何工作的。但是在我的测试中,我时不时得到 400 (BAD REQUEST)。我想知道是什么问题。
这是错误示例:
socket.io.js:3511 POST http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYW0D&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)Request.create @ socket.io.js:3511Request @ socket.io.js:3424XHR.request @ socket.io.js:3352XHR.doWrite @ socket.io.js:3365(anonymous function) @ socket.io.js:3884(anonymous function) @ socket.io.js:4747proxy @ socket.io.js:1197(anonymous function) @ socket.io.js:4762(anonymous function) @ socket.io.js:4742exports.encodePacket @ socket.io.js:4541encodeOne @ socket.io.js:4741eachWithIndex @ socket.io.js:4760map @ socket.io.js:4767exports.encodePayload @ socket.io.js:4746Polling.write @ socket.io.js:3883Transport.send @ socket.io.js:2912Socket.flush @ socket.io.js:2623Socket.sendPacket @ socket.io.js:2683Socket.ping @ socket.io.js:2587(anonymous function) @ socket.io.js:2574
socket.io.js:3511 GET http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYPYb&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)Request.create @ socket.io.js:3511Request @ socket.io.js:3424XHR.request @ socket.io.js:3352XHR.doPoll @ socket.io.js:3382Polling.poll @ socket.io.js:3795Polling.onData @ socket.io.js:3834(anonymous function) @ socket.io.js:3385Emitter.emit @ socket.io.js:4389Request.onData @ socket.io.js:3546Request.onLoad @ socket.io.js:3627xhr.onreadystatechange @ socket.io.js:3499
socket.io.js:3511 POST http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=LOMYeOF&sid=c81baa220764437184a14366357c6d3d 400 (BAD REQUEST)
我的服务器端代码(python flask):
from flask import Flask
from flask_socketio import SocketIO
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret!"
socketio = SocketIO(app)
def root():
return app.send_static_file("index.html")
@socketio.on("connect", namespace="/npTweet")
def connectServer():
print("Client connected")
socketio.emit("connected", namespace="/npTweet")
@socketio.on("startTweets", namespace="/npTweet")
def tweetStreaming():
print("Start streaming tweets...")
socketio.emit("streamTweets", {"stream_result": "test"}, namespace="/npTweet")
if __name__ == "__main__":
socketio.run(app, debug=True)
我的客户端代码:
socket = io.connect("http://" + document.domain + ":" + location.port + "/npTweet");
console.log("http://" + document.domain + ":" + location.port + "/npTweet");
// listen to the event 'connected'
socket.on("connected", function(data){
console.log("listening connected...");
socket.emit("startTweets")
});
socket.on("streamTweets", function(data){
console.log("listen streamTweets...");
console.log(data.stream_result)
});
这些是我启动 index.html 页面时的初始输出:
http://127.0.0.1:5000/npTweet
listening connected...
listen streamTweets...
test
但是大概过了30秒左右,就开始出现之前的400 bad request错误,接着是下面的结果:
listening connected...
listen streamTweets...
test
然后大约 30 秒后,它再次执行错误的请求和结果。我哪里做错了?任何帮助将不胜感激。
对于客户端,我使用的是 socketio js 1.4.5
对于 python: Flask-SocketIO==2.5 python-engineio==0.9.2
这也作为 GitHub 问题发布 here。
根据新的 Flask 0.11 CLI,问题是 Flask 服务器是作为 flask run
启动的。不幸的是,这种启动服务器的方法绕过了 Socket.IO 代码。解决方案是 运行 脚本,以便 if __name__ == 'main':
块内的代码到达 运行.