The first is the main program that saves the session sessions generated by the browser on disk.
#-*-Coding:utf-8-*-
‘‘‘
@author: Cuiruiqiang
@date: 2014-12-3
@filename: countsession.py count Session ' s number
‘‘‘
Import Web
Web.config.debug = False
Urls= (
'/', ' Index ',
'/count ', ' count ',
'/reset ', ' Reset '
)
App = Web.application (Urls,locals ())
#session is being stored on disk. Save the session on your local disk.
session = web.session.Session (App,web.session.diskstore (' Sessions '),
initializer={' Count ': 0})
Class Count:
def GET (self):
Session.count + = 1
Return str (session.count)
Class Reset:
def GET (self):
Session.kill ()
Return ""
if __name__ = = "__main__":
App.run ()
Second, the browser session information is saved in the database MySQL.
The table structure is as follows:
CREATE TABLE Sessions (
session_id Char, UNIQUE not NULL,
Atime timestamp not NULL default Current_timestamp,
Data text)
#-*-coding:utf-8-*-
‘‘‘
@author: Cuiruiqiang
@date: 2014-12-03
@filename: server.py
‘‘‘
Import Web
Web.config.debug = False
Urls= (
'/', ' Index ',
'/count ', ' count ',
'/reset ', ' Reset '
)
App = Web.application (Urls,locals ())
#session is being stored database to store session information in the databases.
db = web.database (dbn= ' MySQL ', db= ' py ', user= ' root ', pw= ' 123456 ')
store = Web.session.DBStore (db, ' sessions ')
Session = Web.session.Session (app,store,initializer={' count ': 0})
Render = Web.template.render (' template/', globals={' context ': Session})
Class Index:
def GET (self):
ReturnRender.index ()
Class Count:
def GET (self):
Session.count + = 1
Return str (session.count)
Class Reset:
def GET (self):
Session.kill ()
Return ""
if __name__ = = "__main__":
App.run ()
The index.html file is as follows:
<!doctype html>
<title>test sessions</title>
<body>
<span>
You is logged in as <b> $context. Count </b>
</span>
</body>
Python implementation session