def POST(self):
render=web.template.render(“templates”) try:
x = web.input(file={}) except ValueError: return “File too large” finally:
return render.upload(“File too large”)
上面这段代码可以限制上传文件的大小在10mb以内
6Session与用户状态
6.1Session
首先要明确一点,在debug模式下,session是无效的。所以为了使用session就要关闭debug模式,也就是在创建程序和模板对象前使用web.config.debug = False。
6.1.1使用session
为了使用session,我们需要把main.py改成如下形式:
import web
web.config.debug = False urls = (“/”, “index”)
app = web.application(urls, globals())
session = web.session.Session(app, web.session.DiskStore(?sessions?), initializer={?count?: 0}) class index: def GET(self):
render=web.template.render(“templates”)
session.count+=1
return render.index(session.count) if __name__ == “__main__”: app.run()
index.html模板改为: $def with(count) $count
这样,页面就会显示count的值,每刷新一次页面,count值就自增1。 解释一下Main.py:
代码第二行就是要关闭debug模式,这样session功能才可以起效。
第五行代码便是创建session的命令,这样本网站的session数据就保存在session对象中。在这里我们用DiskStore类来创建session,表明我们的session是保存在磁盘中的文件里,当程序运行时,就会在项目根目录下出现一个sessions目录,里面的文件就是用于保存session数据的。 Initializer参数是用于初始化session中的数据,在这里就是向session中添加一个名为count的属性,其值为0,然后在后面的代码中就可以用session.count或者session[“count”]的形式访问这个属性值了。
如果要向session中添加新的属性,只需用session.newkey=value或session[“newkey”]=value的形式为属性赋值,然后像访问count属性那样使用新属性就可以了,只不过要注意,在没有为属性赋值前,这个属性没有被初始化,也就是不存在的,直接访问这个属性时会出错的。 如我们可以把index类改为: class index: def GET(self):
render=web.template.render(“templates”)
session.username=”Lisa”
return render.index(session.username)
这里就是向session中添加了一个username属性,并赋值为Lisa,页面最终也就会打印出Lisa的字样。
6.1.2在debug模式下使用session
上文提到了,要使用session必须要把debug模式关闭,否则session不可用,但是这样的话debug下页面被修改后自动重新载入的功能也就无法使用了,这样我们要看修改结果就必须重启服务器,这给开发调试带来了麻烦,为了在debug模式下使用session功能,我们就需要做一些特殊处理。 首先,我们要打开debug模式,也就是web.config.debug = True,如果是在使用web.py内置的服务器,则只需删掉此代码,因为缺省状态下就是debug模式。 再把上节中创建session的那句改成下面的样子就可以解决这个问题了:
if web.config.get(?_session?) is None:
session = web.session.Session(app, web.session.DiskStore(?sessions?), {?count?: 0})
web.config._session = session else:
session = web.config._session
6.1.3在模板中使用session
在模板中使用session同在session使用其他python模块的功能类似,也是在创建render时用到global参数: class index: def GET(self):
render=web.template.render(“templates”,globals={“session”:session}) return render.index()
这样在index.html模板中就能用$session访问session。 比如在index.html改成下面这一句: $session.count
刷新页面显示的就是不断增加的count值。
6.1.4有关session的相关参数修改
web.config. The default values are shown below.
web.config.session_parameters['cookie_name'] = ?webpy_session_id? web.config.session_parameters['cookie_domain'] = None
web.config.session_parameters['timeout'] = 86400, #24 * 60 * 60, # 24 hours in seconds
web.config.session_parameters['ignore_expiry'] = True web.config.session_parameters['ignore_change_ip'] = True
web.config.session_parameters['secret_key'] = ?fLjUfxqXtfNoIldA0A0J? web.config.session_parameters['expired_message'] = ?Session expired? cookie_name – name of the cookie used to store the session id? cookie_domain – domain for the cookie used to store the session id?
timeout – number of second of inactivity that is allowed before the session expires? ignore_expiry – if true, the session timeout is ignored?
ignore_change_ip – if true, the session is only valid when it is accessed from the? same ip address that created the session secret_key – requires explanation?
expired_message – message displayed when the session expires?
6.2Cookie
6.2.1设定cookie属性值
设定cookie属性值用web.setcookie()函数。格式为:
setcookie(name, value, expires=”\ name(string):cookie属性名称 value(string):属性值
expires(int):cookie失效时间,单位是秒,负值表示立刻失效,0表示浏览器关闭时失效,也就是跟session一样
domain(string):此cookie使用的域
secure(bool):如果为True,表示这个cookie必须使用https
比如在代码中我们可以如下为cookie赋值,设定username属性为Lisa,失效时间为1个小时: web.setcookie(“username”,”Lisa”,3600)
另外也可以用类似操作session的形式,如下面后两行都可以用来为cookie中的password属性赋值:
cookies=web.cookies(username=”\ cookies[\ cookies.password=”222222″
6.2.2获取cookie属性值
在代码中获取cookie中username属性值的方法如下: cookies=web.cookies(username=”\ print cookies.username print cookies[\
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库webpy(5)在线全文阅读。
相关推荐: