首页 > 代码库 > web.py session
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="http://www.mamicode.com/test">test</a></br><a href="http://www.mamicode.com/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="http://www.mamicode.com/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="http://www.mamicode.com/logout">Logout</a>‘
return ‘<h1>logout now</h1></br><a href="http://www.mamicode.com/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
web.py session