Python implementation session

Source: Internet
Author: User
Tags sessions

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.