gashero的CherryPy笔记
来源:互联网 发布:手机读取速度软件 编辑:程序博客网 时间:2024/06/05 12:40
gashero的CherryPy笔记
·CherryPy的mount操作
可以看出CherryPy的Tree操作:
cherrypy.tree.mount(Video(),'/video')
=================================================
·CherryPy的位置参数
友好URL处理:
"/admin/user/8173/schedule"的处理方法:
cherrypy.root.admin.user("8173","schedule")
class Admin:
def user(self,*args):
if not args: #空则抛出HTTP错误
raise cherrypy.HTTPError(400,"Error")
args=list(args) #args是tuple,需要转为list
id=args.pop(0)
if args and args[0]=="schedule":
return self.schedule(id)
return "You asked for user '%s'"%id
user.exposed=True
CherryPy2.1中的位置参数只允许出现在default方法中。
=============================================
·CherryPy教程
第一个CP程序:
from cherrypy import cpg
class HelloWorld:
def index(self):
return "Hello, world"
index.exposed=True
cpg.root=HelloWorld()
cpg.server.start(configFile="cp2.cfg")
在Python2.4中提供的发布对象的方法:
@cpg.expose
def index(self):
...
多的一点简单配置文件:
[server]
socketPort=9000
threadPool=10
[session]
storageType=ram
===============================================
·CherryPy路径搜索顺序
"/admin/user/8192/schedule"
试搜索cherrypy.root.admin.user.8192.schedule.index...
cherrypy.root 存在? Yes.
.root.admin 存在? Yes.
.admin.user 存在? Yes.
.user.8192 存在? No.
.user.default 是否可调用+暴露? No.
.admin.user 是否可调用+暴露? Yes.调用它.
"/admin/search/"
试搜索cherrypy.root.admin.search.index...
cherrypy.root 存在? Yes.
.root.admin 存在? Yes.
.admin.search 存在? Yes.
.search.index 存在? Yes.路径到达.
.search.index 是否可调用+暴露? Yes.调用它.
"/admin/unknown"
试搜索cherrypy.root.admin.unknown.index...
cherrypy.root 存在? Yes.
.root.admin 存在? Yes.
.admin.unknown 存在? No.
.admin.default 是否可调用+暴露? No.
.root.admin 是否可调用+暴露? No.
.root.default 是否可调用+暴露? Yes.调用它.
==============================================
·cherrypy学习1
安装python setup.py install
一个test.py:
import cherrypy
class Hello:
def index(self):
return "Hello, world"
index.exposed=True
cherrypy.root=Hello()
cherrypy.server.start()
================================================
·cherrypy学习2
无用的
================================================
·CherryPy主要核心设置选项
[global] server.socket_port
监听端口,默认8080
[global] server.log_file
记录日志,默认关闭
[global] server.log_access_file
存储访问日志,默认是显示到屏幕上
[global] server.log_to_screen
将日志显示到屏幕,默认为True,发布站点最好用False并写入文件
[global] server.log_tracebacks
将跟踪信息写入日志,默认为True。False时只写入500错误
[global] server.max_request_header_size
最大header请求大小(bytes),默认500KB,超过了返回403错误,全局设置,0为不限制。
[global] server.default_content_type
返回信息的缺省类型,默认text/html,(by gashero)全局的
[/path] server.max_request_body_size
最大body请求大小(bytes),默认为100MB。如果请求大于此数字,返回413错误。限制用户上传文件大小的好办法。可以具体到目录。0为不限制。
===============================================
·Session管理的简单例子
import cherrypy
class Sessiontest:
def index(self):
#用词典的get()方法
count=cherrypy.session.get('count',0)+1
cherrypy.session['count']=count #更新计数
return "count=%d"%count
index.exposed=True
cherrypy.root=Sessiontest()
cherrypy.config.update({'session_filter.on':True})
cherrypy.server.start()
注意,需要将session_filter.on设置为True。
cherrypy.session为词典对象。有个"_id"关键字用于存取Session的ID。
=============================================
·在CherryPy应用中使用Session
首先修改配置文件:
[global]
sessionFilter.on=True
sessionFilter.storageType="File"
sessionFilter.storagePaht="/sessions"
sessionFilter.Timeout=60
或者写入到代码中:
cherrypy.config.update({'session_filter.on':True,
'sessionFilter.storageType'="File",
'sessionFilter.storagePath'="/sessions",
'sessionFilter.Timeout':60})
==============================================
·适用于CGI程序员的CherryPy
版本太老,略
++++++++++++++++++++++++++++++++++++++++++++++
·临时笔记
·使用Cheetah模板:
template=Template("""
<>... ...
""",[locals(),globals()])
return template.respond()
·返回静态文件的方法
在处理器中返回文件的句柄即可
对于图片等等二进制文件,必须使用'rb'打开方式。
注意,在调试状态下,会因为CherryPy自动给每一个文件末尾加上调试信息,如文件大小和处理时间,所以小心文件的被修改。将服务器设置为发行状态即可。
处理静态文件比较合适的方法是使用default方法的参数。
小心CGI脚本攻击,注意检测输入参数中的'..'、'/'等等参数。但是,经测试表明,输入如上两个参数将会直接导致CherryPy的崩溃,(by gashero)而不是返回上一级文件。
可以考虑设置一个词典,键为发布文件名,值为磁盘文件名。来建立一个映射,这样可以很好的解决发布参数被攻击的问题,还可以有限制的发布。
++++++++++++++++++++++++++++++++++++++++++++++
·CherryPy的自带教程
使用的tutorial.conf文件:
[global]
server.socketPort=8080
server.threadPool=10
server.environment="production"
# server.showTracebacks=True
# server.logToScreen=False
·01 Hello World
import cherrypy
class HelloWorld:
def index(self):
return "Hello, world"
index.exposed=True
cherrypy.root=HelloWorld()
if __name__=='__main__':
cherrypy.config.update(file='tutorial.conf')
cherrypy.server.start()
·02 expose method
略
·03 get and post
def greetuser(self,name=None):
if name:
return "nihao"
else:
if name is None:
return "请输入名字"
else:
return "名字错误"
greetuser.exposed=True
·04 complex site
略
·05 derived objects
继承对象互相嵌套HTML代码
·06 default method
使用default方法的参数获取/*的路径参数,参数名随意
·07 sessions
def index(self):
count=cherrypy.session.get('count',0)+1
cherrypy.session['count']=count
return count
·08 generators and yield
使用yield语句分阶段返回数据
yield "..."
·09 files
上传的文件暂时存放在磁盘中,CherryPy将会把文件作为参数传递给方法,此参数将会有个file属性,指向上传文件的打开句柄。如果需要保存文件,则需要用read()方法读取,并write()到指定文件。
发送文件到客户端可以直接返回文件句柄,CherryPy将会读取文件并作为HTTP内容发送到客户端。如果需要告知客户端一个需要保存的文件,(by gashero)则需要cherrypy.lib.cptools.serveFile方法,此方法有四个参数:
serveFile(path,contentType=None,disposition=None,name=None)
name是服务器端文件名。当disposition省略时则name无效,disposition通常为attachment。可以手动设置contentType,可以设置为"application/x-download"来让客户端保存文件。
import os
localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)
import cherrypy
from cherrypy.lib import cptools
class FileDemo(object):
def download(self):
path = os.path.join(absDir, "pdf_file.pdf")
return cptools.serveFile(path, "application/x-download",
"attachment", os.path.basename(path))
download.exposed = True
·10 HTTP errors
用于显示HTTP错误
HTTP重定向:
raise cherrypy.HTTPRedirect('/')
指定错误码:
raise cherrypy.HTTPError(status=code)
指定错误码和错误信息:
raise cherrypy.HTTPError(code,message=msg)
更改错误缺省页面:
import os
localDir = os.path.dirname(__file__)
curpath = os.path.normpath(os.path.join(os.getcwd(), localDir))
cherrypy.config.update({'error_page.403' : os.path.join(curpath, "custom_error.html")})
- gashero的CherryPy笔记
- gashero的pypcap实践笔记
- gashero的mod_python开发经验
- CherryPy的Hello World分析
- CherryPy wsgiserver模块的分析
- CherryPy的Hello World分析
- web framework---cherrypy学习笔记1
- CherryPy发布静态文件的方法
- 基于python的web开发框架-CherryPy
- 使用 CherryPy 作为Django的Development Server
- 使用 CherryPy 作为Django的Development Server
- 关于cherrypy和wsgidav的一些问题
- CherryPy——基于Python的Web框架
- cherrypy + eventlet 完成复杂的异步任务处理
- CherryPy architecture
- CherryPy 解析
- cherryPY 入门
- CherryPy 入门
- 怎样移植你的POWERPLAY应用程序
- 用Alter命令修改Mysql資料表中欄位的形態7/8
- Mysql:用grant將資料庫中某些表授權給用戶7/8
- F-22、漂移与飞碟
- Mysql:用phpmyadmin匯出資料至XLS時中文亂碼7/8
- gashero的CherryPy笔记
- Duwamish 7.0学习笔记
- Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.soc
- web应用程序测试方法和测试技术详述
- CherryPy发布静态文件的方法
- jdbc连接各种数据库方式列表
- 恐怖袭击新方式:用笔记本电脑炸飞机
- CSS里面运行脚本
- 外部.js文件和外部.css文件