基於hi-nginx的web開發(python篇)——cookie和會話管理

來源:互聯網
上載者:User

標籤:color   serve   通過   post   main   log   需要   art   session   

hi-nginx通過redis管理會話。

要開啟管理,需要做三件事。

第一件開啟userid:

        userid                  on;        userid_name             SESSIONID;        userid_domain           localhost;        userid_path             /;        userid_expires          300s;

這個功能是nginx內建的,可以直接使用。需要注意的是,hi-nginx只認識SESSIONID的userid_name。

第二件是配置redis伺服器:

        hi_redis_host 127.0.0.1;        hi_redis_port 6379;

當然,你應該先安裝redis並確保它運行。

第三件是在location段開啟會話管理:

    location  /  {            hi_need_session on;            hi_session_expires 300s;            hi_python_script python/index.py;    }

整個nginx配置寫下來,就是:

 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 }

需要注意是,應該確保hi_session_expires和userid_expires的值保持一致。

配置寫完後,記得reload或者restart nginx。

 

接下來就是使用會話管理的api了。

說來太簡單,都不好意思寫出來,用req.has_session,req.get_session和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)

 

那麼,cookie怎麼辦?人們使用cookie的一大用途建立會話機制。上文已經把會話管理的使用說清楚了。所以使用hi.py架構時不需要特別留意cookie的管理。當然,如果你想自己管理cookie,hi-nginx也提供req.has_cookie和req.get_cookie兩個唯讀api。如果要寫api,可以使用res.header來寫。比如:

在location段中添加hi_need_cookies on

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     }

在操作函數中在添加相關操作:

@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)

如上所見,在hi.py中操控和管理cookie和會話是非常方便的,用來寫個登陸或者購物車什麼的,配合hi.py的jinja2模板引擎,簡直易如反掌。

 

基於hi-nginx的web開發(python篇)——cookie和會話管理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.