webpy(6)

2019-04-21 10:30

在这里获取cookie对象的时候为username指定了一个缺省值,如果当前cookie还没有这个属性,则创建此属性并赋值为这里的缺省值,而如果不指定属性缺省值,同时这个属性还不存在的话,则在后面访问这个username属性时会抛出异常。

另外一种获取属性的方法是使用get函数,此时如果username属性不存在的话,函数返回None而不是抛出异常,如:。 print cookie.get(“username”)

7高级应用

7.1Ctx

Web.ctx可以用于获取一些有用的上下文变量。

比如下面代码可以返回当前浏览器发来的header中的引用页信息,如果没有则返回默认的google.com:

web.ctx.env.get(?HTTP_REFERER?, ?http://google.com?) 其他可以从ctx中获取的信息包括: Request

environ a.k.a. env – a dictionary containing the standard WSGI environment variables home – the base path for the application, including any parts “consumed” by outer applications http://example.org/admin

homedomain – ? (appears to be protocol + host) http://example.org

homepath – The part of the path requested by the user which was trimmed off the current app. That is homepath + path = the path actually requested in HTTP by the user. E.g. /admin host – the hostname (domain) and (if not default) the port requested by the user. E.g. example.org, example.org:8080

ip – the IP address of the user. E.g. xxx.xxx.xxx.xxx method – the HTTP method used. E.g. GET

path – the path requested by the user, relative to the current application. If you are using subapplications, any part of the url matched by the outer application will be trimmed off. E.g. you have a main app in code.py, and a subapplication called admin.py. In code.py, you point /admin to admin.app. In admin.py, you point /stories to a class called stories. Within stories, web.ctx.path will be /stories, not /admin/stories. E.g. /articles/845 protocol – the protocol used. E.g. https

query – an empty string if there are no query arguments otherwise a ? followed by the query string. E.g. ?fourlegs=good&twolegs=bad

fullpath a.k.a. path + query – the path requested including query arguments but not including homepath. E.g. /articles/845?fourlegs=good&twolegs=bad Response

status – the HTTP status code (default ?200 OK?) 401 Unauthorized headers – a list of 2-tuples containing HTTP headers output – a string containing the response entity

7.2Application processors

web.py 提供的processors 可以在request执行前和执行后进行一些处理工作。 def my_processor(handler): print ?before handling? result = handler() print ?after handling? return result

app.add_processor(my_processor)

或者使用hooks 和 unload 实现在request开始和结束的时候进行某些处理工作。 def my_loadhook():

print “my load hook”

def my_unloadhook(): print “my unload hook”

app.add_processor(web.loadhook(my_loadhook)) app.add_processor(web.unloadhook(my_unloadhook)) 在hook 函数中还可以使用global变量,如 web.header() def my_loadhook():

web.header(?Content-type?, “text/html; charset=utf-8″)

app.add_processor(web.loadhook(my_loadhook))

7.3自定义NotFound页面

首先,我们先定义一个函数如下: def notfound():

return web.notfound(“Sorry, the page you were looking for was not found.”) 然后把这个函数赋给app的notfound属性: app.notfound = notfound

这样,当用户访问了不存在的网页的时候就会在页面上打印出notfound返回语句中notfound函数中的参数那句话了。

当然,也可以使用模板定义一个更复杂一些的页面,比如我们可以在templates目录下新建一个notfound.html文件作为模板,比如是这样:

$def with(message)

NotFound $:message

然后把notfound函数改为如下形式: def notfound():

render=web.template.render(“templates”)

return web.notfound(render.notfound(“Sorry, the page you were looking for was not found. “)) 类似的我们也可以定义内部错误页面: def internalerror():

return web.internalerror(“Bad, bad server. No donut for you.”)

app.internalerror = internalerror

7.4在Google App Engine上使用web.py

Google App Engine是支持使用web.py的,使用方法是把web.py安装目录,也就是python安装目录下的Lib\\site-packages\\web目录,整个拷贝到GAE的项目中,把app.run()改成app.cgirun(),然后上传到GAE上,就可以用web.py开发GAE程序了。

但是要注意的是,由于某种安全原因,在GAE中是不能直接使用web.py的模板的,必须在本地编译后才上传到GAE服务器上才能使用web.py模板,比如说我们有一个GAE项目叫

webpyongae,webpyongae/web目录就是拷贝进来的web.py程序,webpyongae/templates为放置模板的目录,那么编译的方法就是在webpyongae目录下,执行python /web/template.py –complie templates。

在http://webpy.appspot.com/ 这个GAE网站上就运行着一个简单的web.py示例程序,并且在http://webpy.appspot.com/source此页中可以查看这个站点首页的代码。

8其他

8.1解决css在windows下解析过慢的问题

web.py3.*如果使用到了独立于html之外的css文件,页面加载的时间会比较长,其原因是webpy对windows格式的换行无法正常的解析,无法判断css文件的实际长度,所以服务器会一直等待直到超时。解决方法如下:

#解决windows下css文件解析太慢的问题–>>

from SimpleHTTPServer import SimpleHTTPRequestHandler

send_head = SimpleHTTPRequestHandler.send_head

def new_send_head(*a, **kw): print ?new_send_head?, a, kw f = send_head(*a, **kw)


webpy(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2016年幼儿教育学知识考点

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: