web.py Session Learning:
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 ' 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 '
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 ' Return '
if __name__ = = ' __main__ ':
App.run ()
The results are:
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, April 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
The browser will automatically redirect
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, APR 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
Enter User name password:
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, APR 15:33:48 GMT
location:http://localhost:8080/
Server:localhost
set-cookie:system_mangement=; Expires=fri, APR 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