1. 请解释什么是转发与重定向,它们有什么区别
转发:对web用户是透明的,或者说在Web浏览器中的Url是不会变的,在服务器会根据Url去读取特定的资源,并将资源的内容返回给客户端
http://localhost:5000/test.tml
服务端资源对于用户不一定是可访问的
重定向:用户是可见的,Web浏览器地址栏中的Url将改变
http://localhost:5000/test.tml
http://localhost:5000/abc.tml
服务端资源必须是可访问的
2. 在Flask中如何转发和重定向资源
from flask import *
app = Flask(__name__)
# 转发
@app.route('/test')
def test():
return app.send_static_file('test1.txt')
# 重定向
@app.route('/abc')
def abc():
return redirect('static/test1.txt')
if __name__ == '__main__':
app.run()
总结
转发是读取指定资源的内容,然后发送给客户端,浏览器的Url是不变的。
而重定向会导致浏览器的Url发生变化,也就是让浏览器重新访问另一个Url。
浙公网安备 33010602011771号