web.py session

web.py session学习:

import web
from web import form

urls = (
'/','Index',
'/test','Test',
'/login','Login',
'/logout','Logout',
)

render = web.template.render(".")

allowed = (
('admin','123123'),
)

web.config.debug = False
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'))


class Index:
def GET(self):
if session.get('logged_in',False):
return '<h1>Login Success!!!</h1><a href="/test">test</a></br><a href="/logout">Logout</a>'
raise web.seeother('/login')

class Login:
def GET(self):
return render.login()
def POST(self):
i = web.input()
username = i.get('username')
passwd = i.get('passwd')
if (username,passwd) in allowed:
session.logged_in = True
web.setcookie('system_mangement', '', 60)
raise web.seeother('/')
else:
return '<h1>Login Error!!!</h1></br><a href="/login">Login</a>'

class Logout:
def GET(self):
session.logged_in = False
raise web.seeother("/login")

class Test:
def GET(self):
if session.get('logged_in',False):
return '<h1> test login success!!!</h1></br><a href="/logout">Logout</a>'
return '<h1>logout now</h1></br><a href="/login">Login</a>'


if __name__ == '__main__':
app.run()


结果有:

GET http://localhost:8080/

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/
Request Method:GET
Status Code:303 See Other
Response Headers
view source
Content-Type:text/html
Date:Fri, 07 Apr 2017 15:30:27 GMT
Location:http://localhost:8080/login
Server:localhost
Set-Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a; Path=/; httponly
Transfer-Encoding:chunked
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive #正常情况下第一次请求是没有cookie的
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

浏览器会自动重定向
GET http://localhost:8080/login
Gereral:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/login
Request Method:GET
Status Code:200 OK

Response Headers
view source
Content-Type:text/html; charset=utf-8
Date:Fri, 07 Apr 2017 15:30:27 GMT
Server:localhost
Set-Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a; Path=/; httponly
Transfer-Encoding:chunked

Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

输入用户名密码:
POST http://localhost:8080/login {‘username’:admin,'passwd':123123}
Gereral:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/login
Request Method:POST
Status Code:303 See Other
Response Headers
view source
Content-Type:text/html
Date:Fri, 07 Apr 2017 15:33:48 GMT
Location:http://localhost:8080/
Server:localhost
Set-Cookie:system_mangement=; expires=Fri, 07 Apr 2017 15:34:48 GMT; Path=/
Set-Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a; Path=/; httponly
Transfer-Encoding:chunked
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:28
Content-Type:application/x-www-form-urlencoded
Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/login
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Form Data
username:admin
passwd:123123

 

转载于:https://www.cnblogs.com/a280723148/p/6680591.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值