python session 使用dbstore 防止eoferror

官方api中没有发现配置连接字符串的地方,很是郁闷,莫非只能连接服务器所在的机器的数据源么,经过几天自己的摸索,终于发现了  啥也不说了,直接上代码 

#    shelfstore 模式开始
#    store = web.session.ShelfStore(shelve.open('session.shelf'))
#    shelfstore 模式结束
#    dbstore 模式开始
param = get_ini(os.path.join('conf', 'Configure.ini'))
addr = param.Database.addr
port = param.Database.port
User = param.Database.User
Password = Decrypt(str(param.Database.Password))
db = web.database(dbn='mysql',host=addr, port=port, db='venus_sbl', user=User, pw=Password, charset='utf8')
store = web.session.DBStore(db, 'sessions')
#    dbstore 模式结束
#    diskstore 模式开始
#store = web.session.DiskStore('sessions')
#    diskstore 模式结束
session = web.session.Session(app, store, initializer={'user': 'anonymous', 'ticket':''})

现在是Configure.ini的信息

[Database]
addr = 192.168.56.195
port = 3306
User = root
Password = f93a01f30b2d2323

注意数据库中要创建sessions 表哦  以下是建表语句
DROP TABLE IF EXISTS `sessions`;

CREATE TABLE `sessions` (
  `session_id` char(128) character set gbk NOT NULL,
  `atime` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `data` mediumtext character set gbk,
  UNIQUE KEY `session_id` (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


你可能感兴趣的:(python,session,dbstore)