1. 请解释什么是转发与重定向,它们有什么区别?
- 转发:
- 对用户是透明的,或者在Web浏览器中的Url是不会变的,在服务端会根据请求Url去读取特定的资源,并将资源的内容返回给客户端
- 服务端资源对于用户不一定是可访问的
- http://localhost:5000/test.html
- 重定向
- 用户是可见的,Web浏览器地址栏中的Url将改变
- 服务端资源必须是可访问的
- http://localhost:5000/test.html
- http://localhost:5000/abc.html
2. 在Falsk中如何转发和重定向资源
test1.txt
Hello world!
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()
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [31/Mar/2020 16:09:03] "GET /test HTTP/1.1" 404 -