Web development based on hi-nginx (python)-cookie and session management, hi-nginxpython
Hi-nginx manages sessions through redis.
To enable management, you need to do three things.
First, enable userid:
userid on; userid_name SESSIONID; userid_domain localhost; userid_path /; userid_expires 300s;
This function is built in nginx and can be used directly. Note that hi-nginx only recognizes userid_name of SESSIONID.
The second is to configure the redis Server:
hi_redis_host 127.0.0.1; hi_redis_port 6379;
Of course, you should first install redis and make sure it runs.
The third is to enable session management in the location segment:
location / { hi_need_session on; hi_session_expires 300s; hi_python_script python/index.py; }
Write down the nginx Configuration:
1 server { 2 listen 8080; 3 server_name localhost; 4 5 userid on; 6 userid_name SESSIONID; 7 userid_domain localhost; 8 userid_path /; 9 userid_expires 300s;10 11 hi_redis_host 127.0.0.1;12 hi_redis_port 6379;13 14 15 location / {16 hi_need_cache off;17 hi_cache_expires 5s;18 19 hi_need_session on;20 hi_session_expires 300s;21 hi_python_script python/index.py;22 }23 }
Make sure that the values of hi_session_expires and userid_expires are consistent.
After the configuration is completed, remember to reload or restart nginx.
The next step is to use the session management api.
It is too simple to write it out. Use req. has_session, req. get_session and res. session:
@app.route('^/session/?$',['GET'])def session(req,res,param): k='test' v=0 if req.has_session(k): v=int(req.get_session(k)) res.session(k,str(v+1)) else: res.session(k,str(v)) res.content('{}={}'.format(k,v)) res.status(200)
So what should I do with cookies? One of the major uses of cookies is to establish a session mechanism. The usage of session management has been clearly stated above. Therefore, you do not need to pay special attention to cookie management when using the hi. py framework. Of course, if you want to manage cookies by yourself, hi-nginx also provides two read-only APIs: req. has_cookie and req. get_cookie. To write an api, you can use res. header to write the api. For example:
Add hi_need_cookies on in location
1 location / {2 hi_need_cache off;3 hi_cache_expires 5s;4 hi_need_cookies on;5 hi_need_session on;6 hi_session_expires 300s;7 hi_python_script python/index.py;8 }
Add related operations in the operation function:
@app.route('^/session/?$',['GET'])def session(req,res,param): k='test' v=0 if req.has_session(k): v=int(req.get_session(k)) res.session(k,str(v+1)) else: res.session(k,str(v)) cv=v if req.has_cookie(k): cv=int(req.get_cookie(k)) res.header('Set-Cookie','{}={};Path={};Domain={}'.format(k,cv+1,'/','localhost')) else: res.header('Set-Cookie','{}={};Path={};Domain={}'.format(k,cv,'/','localhost')) res.content('session:{0}={1},cookie:{0}={2}'.format(k,v,cv)) res.status(200)
As you can see above, it is very convenient to control and manage cookies and sessions in hi. py. It is easy to use to write a login or shopping cart or something, and work with the jinja2 template engine of hi. py.