Python 在socket.io事件完成后,Flask socket.io重定向到模板
我收到一些socket.io事件。事件处理后,我想将用户重定向到某个页面。但是,重定向不起作用。我不知道怎么了。下面是我的代码:Python 在socket.io事件完成后,Flask socket.io重定向到模板,python,flask,socket.io,Python,Flask,Socket.io,我收到一些socket.io事件。事件处理后,我想将用户重定向到某个页面。但是,重定向不起作用。我不知道怎么了。下面是我的代码: from flask import Flask, render_template, send_from_directory, redirect, url_for from flask_socketio import SocketIO, emit import base64 import os import random, string app = Flask(__n
from flask import Flask, render_template, send_from_directory, redirect, url_for
from flask_socketio import SocketIO, emit
import base64
import os
import random, string
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
try:
image_names = os.listdir('./images')
print image_names
return render_template("gallery.html", image_names=image_names)
except Exception as ex:
print ex
@socketio.on('takephoto')
def takePhoto(*args):
try:
decoded = base64.b64decode(args[0])
filename = ''.join(random.choice(string.lowercase) for x in range(6)) + '.jpg'
with open("./images/" + filename, "wb") as fh:
fh.write(args[0].decode('base64'))
except Exception, ex:
print ex
redirect(url_for('index')) #This doesnt work
#how can i go to index from this point?
if __name__ == '__main__':
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app.debug = True
server = pywsgi.WSGIServer(('', 5001), app, handler_class=WebSocketHandler)
server.serve_forever()
我认为您缺少
return
语句
return redirect(url_for('index'))
我认为情况并非如此,因为socket.io上下文中的return语句将向客户端返回确认,而不是向浏览器返回呈现的页面。我需要一些类似人工的东西,也许吧?或者像app.navigate_to(url)之类的东西。我也在想,将上下文切换到app可能会有所帮助,但我真的不知道怎么做。我也需要这个!经过一番搜寻,我找到了。这是你想要的吗?