CherryPy具有更细致的配置机制,可以在各个层次设置不同的配置
server.socket_port: 9090
cherrypy.config.update("server.conf")
cherrypy.quickstart(myapp, '/', {'/': {'tools.gzip.on': True}})
tool.gzip.on: True
cherrypy.quickstart(myaoo, '/', 'app.conf')
class Root(object):
@cherrypy.expose
@cherrypy.tools.gzip()
def index(self):
return "hello world!"
class Root(object):
@cherrypy.expose
def index(self):
return "hello world!"
index._cp_config = {'tools.gzip.on': True}
tools.gzip.on: True
key = "..."
appid = "..."
class Root(object):
@cherrypy.expose
def index(self):
google_appid = cherrypy.request.app.config['googleapi']['appid']
return "hello world!"
cherrypy.quickstart(Root(), '/', "app.conf")
cherrypy.response.cookie[key] =value
cherrypy.response.cookie[key]['expires'] = 0
import cherrypy
class MyCookieApp(object):
@cherrypy.expose
def set(self):
cookie = cherrypy.response.cookie
cookie['cookieName'] = 'cookieValue'
cookie['cookieName']['path'] = '/'
cookie['cookieName']['max-age'] = 3600
cookie['cookieName']['version'] = 1
return "<html><body>Hello, I just sent you a cookie</body></html>"
@cherrypy.expose
def read(self):
cookie = cherrypy.request.cookie
res = """<html><body>Hi, you sent me %s cookies.<br />
Here is a list of cookie names/values:<br />""" % len(cookie)
for name in cookie.keys():
res += "name: %s, value: %s<br>" % (name, cookie[name].value)
return res + "</body></html>"
if __name__ == '__main__':
cherrypy.quickstart(MyCookieApp(), '/cookie')
tools.sessions.on: True
cherrypy.quickstart(myapp, '/', "app.conf")
import cherrypy
@cherrypy.expose
def index(self):
if 'count' not in cherrypy.session:
cherrypy.session['count'] = 0
cherrypy.session['count'] += 1
tools.sessions.on:True
tools.sessions.storage_class = cherrypy.lib.sessions.FileSession
tools.sessions.storage_path = “/ some / directory”
tools.sessions.on:True
tools.sessions.storage_class = cherrypy.lib.sessions.MemcachedSession
import mimetypes
mimetypes.types_map['.csv'] = 'text/csv'
[/style.css]
tools.staticfile.on = True
tools.staticfile.filename = “/home/site/style.css”
[/ static]
tools.staticdir.on = True
tools.staticdir.dir = “/ home / site / static”
[/]
tools.staticdir.root = “/ home / site”
[/ static]
tools.staticdir.on = True
tools.staticdir.dir = “static”
[/ static]
tools.staticdir.on = True
tools.staticdir.dir = “/ home / site / static”
tools.staticdir.index = “index.html”
from cherrypy.lib.static import serve_file
@cherrypy.expose
def download(self, filepath):
return serve_file(filepath, "application/x-download", "attachment")
class Root(object):
@cherrypy.expose
@cherrypy.tools.json_in()
def index(self):
data = cherrypy.request.json
class Root(object):
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self):
return {'key': 'value'}
from cherrypy.lib import auth_basic
USERS = {'jon': 'secret'}
def validate_password(realm, username, password):
if username in USERS and USERS[username] == password:
return True
return False
conf = {
'/protected/area': {
'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'localhost',
'tools.auth_basic.checkpassword': validate_password
}
}
cherrypy.quickstart(myapp, '/', conf)
from cherrypy.lib import auth_digest
USERS = {'jon': 'secret'}
conf = {
'/protected/area': {
'tools.auth_digest.on': True,
'tools.auth_digest.realm': 'localhost',
'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(USERS),
'tools.auth_digest.key': 'a565c27146791cfb'
}
}
cherrypy.quickstart(myapp, '/', conf)
import cherrypy
class HelloWorld(object):
@cherrypy.expose
def index(self):
return "Hello World!"
if __name__ == '__main__':
cherrypy.quickstart(HelloWorld(), '/',
{
'/favicon.ico':
{
'tools.staticfile.on': True,
'tools.staticfile.filename': '/path/to/myfavicon.ico'
}
}
)
[/favicon.ico]
tools.staticfile.on: True
tools.staticfile.filename: "/path/to/myfavicon.ico"
import cherrypy
class HelloWorld(object):
@cherrypy.expose
def index(self):
return "Hello World!"
if __name__ == '__main__':
cherrypy.quickstart(HelloWorld(), '/', app.conf)