Hi-nginx-based Web development (Python)--cookie and Session Management

Source: Internet
Author: User
Tags install redis redis server

Hi-nginx management sessions through Redis.

To start management, there are three things to do.

The first thing to open the 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. It should be noted that Hi-nginx only know the userid_name of SessionID.

The second piece is to configure the Redis server:

        Hi_redis_host 127.0.0.1;        Hi_redis_port 6379;

Of course, you should install Redis first and make sure it runs.

The third is to open session management at the location section:

Location   /  {            hi_need_sessionon;            Hi_session_expires 300s;            Hi_python_script python/index.py;    }

The entire Nginx configuration is written down:

1 server {2Listen 8080;3server_name localhost;4 5UserID on;6Userid_name SESSIONID;7Userid_domain localhost;8Userid_path/;9Userid_expires 300s;Ten  OneHi_redis_host 127.0.0.1; AHi_redis_port 6379; -  -      the Location /{ -Hi_need_cache off; -Hi_cache_expires 5s; -  +Hi_need_session on; -Hi_session_expires 300s; +Hi_python_script python/index.py; A     } at}

It is important to be aware that the values of hi_session_expires and userid_expires should be kept consistent.

After the configuration is finished, remember reload or restart Nginx.

The next step is to use the Session Management API.

Say too simple, all embarrassed to write, with Req.has_session,req.get_session and res.session can:

@app. Route ('^/session/?$',['GET'])defsession (Req,res,param): K='Test'v=0ifReq.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 about cookies? People use cookies for a great purpose to establish conversational mechanisms. The use of Session management has been clearly explained above. Therefore, there is no need to pay particular attention to the management of cookies when using the hi.py framework. Of course, if you want to manage your own Cookie,hi-nginx also provides Req.has_cookie and Req.get_cookie two read only APIs. If you want to write an API, you can write it using Res.header. Like what:

Add Hi_need_cookies on to the location segment

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     }

In the action function, add the related action:

@app. Route ('^/session/?$',['GET'])defsession (Req,res,param): K='Test'v=0ifReq.has_session (k): v=Int (req.get_session (k)) Res.session (K,str (v+1))    Else: Res.session (K,str (v)) CV=vifReq.has_cookie (k): CV=Int (Req.get_cookie (k)) Res.header ('Set-cookie','{}={}; path={};D omain={}'. Format (k,cv+1,'/','localhost'))    Else: Res.header ('Set-cookie','{}={}; path={};D omain={}'. Format (K,CV,'/','localhost')) Res.content ('session:{0}={1},cookie:{0}={2}'. Format (K,V,CV)) Res.status (200)

As you can see, it is very handy to manipulate and manage cookies and conversations in hi.py, to write a landing or shopping cart, and to work with hi.py's jinja2 template engine.

Hi-nginx-based Web development (Python)--cookie and Session Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.