zxy1013 / flaskblog Goto Github PK
View Code? Open in Web Editor NEWA project for using blog
A project for using blog
(1)cookie 在网站中,HTTP请求是无状态的。也就是说,即使第一次用户访问服务器并登录成功后,第二次请求服务器依然不知道当前发起请求的是哪个用户。cookie的出现就是为了解决这个问题,第一次登录后服务器返回一些数据(cookie)给浏览器,浏览器将这些数据保存在本地。当用户发起第二次请求的时候,浏览器自动的将上次请求得到的cookie数据携带给服务器,服务器通过这些cookie数据就能分辨出当前发起请求的是哪个用户了。cookie存储的数据量有限,不同的浏览器有不同的存储大小,但一般不超过4K,因此使用cookie只能存储一些少量的数据。在响应头中添加。 (2)session session与cookie的作用有点类似,都是为了存储用户相关的信息。不同的是,cookie是存储在本地浏览器,session存储在服务器。存储在服务器的数据会更加的安全,不容易被窃取。但存储在服务器也有一定的弊端,就是会占用服务器的资源。 (3)cookie和session的结合使用 web开发发展至今,cookie和session的使用已经出现了一些非常成熟的方案。在如今的市场和企业里,一般有两种存储方式: - 存储在服务器:通过cookie存储一个session_id,然后具体的数据则保存在session中。当用户已经登录时,会在浏览器的cookie中保存一个session_id,下次再次请求的时候,会把session_id携带上来,服务器根据session_id在session库中获取用户的session数据,从而能够辨别用户身份,以及得到之前保存的状态信息。这种专业术语叫做server side session - 将session数据加密,然后存储在cookie中。这种专业术语叫做client side session,flask采用的就是这种方式,但是也可以替换成其它形式。 实现方式: 1. cookie方式: - 保存: ``` 通过response对象保存。 response = redirect(xxx) response = render_template(xxx) response = Response() response = make_response() response = jsonify() 通过对象调用方法 response.set_cookie(key,value,max_age) 其中max_age表示过期时间,单位是秒 也可以使用expires设置过期时间,expires=datetime.now()+timedelta(hour=1) ``` - 获取: ``` 通过request对象获取。 request.form.get() request.args.get() cookie也在request对象中 request.cookies.get(key) ----> value ``` - 删除: ``` 通过response对象删除。 把浏览器中的key=value删除了 response = redirect(xxx) response = render_template(xxx) response = Response() response = make_response() response = jsonify() 通过对象调用方法 response.delete_cookie(key) ``` 2. session方式: session是在服务器端进行用户信息的保存。一个字典 注意:使用session必须要设置配置文件,在配置文件中添加SECRET_KEY='xxxxx',添加SECRET_KEY的目的就是用于sessionid的加密。如果不设置会报错。 - 设置: 如果要使用session,需要直接导入:from flask import session 把session当成字典使用,因此:session[key]=value 就会将key=value保存到session的内存空间 并会在响应的时候自动在response中自动添加一个cookie:session=用SECRET_KEY加密后的id - 获取 用户请求页面的时候就会携带上次保存在客户端浏览器的cookie值,其中包含session=加密后的id 获取session值的话通过session直接获取,因为session是一个字典,就可以采用字典的方式获取即可。 value = session[key] 或者 value = session.get(key) 这个时候获取session内容,底层会自动解密获取的值,进行查找并找到对应的session空间 - 删除 session.clear() 删除session的内存空间和cookie,客户端和服务器都删除 (推荐使用) del session[key] 只会删除session中的这个键值对,不会删除session空间和cookie 3.登录权限的验证 只要走center路由,判断用户是否是登录状态,如果用户登录了,可以正常显示页面,如果用户没有登录 则自动跳转到登录页面进行登录,登录之后才可以进行查看。 钩子函数: 直接应用在app上: before_first_request before_request after_request teardown_request 应用到蓝图: before_app_first_request 只在第一次请求中执行 before_app_request 在所有请求中执行 after_app_request 处理请求后 执行after_app_request teardown_app_request 在after_app_request后执行 安装缓存包: pip install redis pip install flask-caching 使用缓存: 1. Cache对象 exts的init文件中 from flask_caching import Cache cache = Cache() 2. apps的init文件中 from exts import cache config = { 'CACHE_TYPE': 'redis', 'CACHE_REDIS_HOST': '127.0.0.1', 'CACHE_REDIS_PORT': 6379 } def create_app(): cache.init_app(app,config) 设置键值对缓存: cache.set(key,value,timeout=second) cache.set_many([(key,value),(key,value),(key,value),...]) 获取缓存值: cache.get(key) cache.get_many(key1,key2,...) 删除缓存: cache.delete(key) cache.delete_many(key1,key2,...) cache.clear() 视图函数缓存:50s内访问/时不从后台访问,而是取缓存中的内容,超过50s后再进行重新加载 @app.route("/") @cache.cached(timeout=50) def index(): return render_template('index.html') 启动redis后才可进行连接 进入D:\wendang\redis 输入cmd redis-server redis.windows.conf
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.