def setup_server():
def break_header():
# Add a header after finalize that is invalid
cherrypy.serving.response.header_list.append((2, 3))
cherrypy.tools.break_header = cherrypy.Tool(
'on_end_resource', break_header)
class Root:
@cherrypy.expose
def index(self):
return "hello"
@cherrypy.config(**{'tools.break_header.on': True})
def start_response_error(self):
return "salud!"
@cherrypy.expose
def stat(self, path):
with cherrypy.HTTPError.handle(OSError, 404):
st = os.stat(path)
root = Root()
cherrypy.tree.mount(root)
python类Tool()的实例源码
def testWarnToolOn(self):
# get
try:
cherrypy.tools.numerify.on
except AttributeError:
pass
else:
raise AssertionError("Tool.on did not error as it should have.")
# set
try:
cherrypy.tools.numerify.on = True
except AttributeError:
pass
else:
raise AssertionError("Tool.on did not error as it should have.")
def __init__(self):
"""
The Email Tool provides functionality for sending password
reset emails in the background rather than blocking
the request. The user should not be made aware of success
or failure, so all error handling is logged.
"""
cherrypy.Tool.__init__(self, 'on_end_request',
self._send_email,
priority=80)
def __init__(self):
cherrypy.Tool.__init__(self, 'before_finalize',
self._render,
priority=10)
def __init__(self):
cherrypy.Tool.__init__(self, "on_start_resource",
self._check_method,
priority=10)
def __init__(self):
cherrypy.Tool.__init__(self, 'on_end_request', self.record_stop)
def _setup(self):
"""Hook this tool into cherrypy.request.
The standard CherryPy request object will automatically call this
method when the tool is "turned on" in config.
"""
if appstats.get('Enabled', False):
cherrypy.Tool._setup(self)
self.record_start()
def run_app(listen_address="0.0.0.0:8001"):
host, port = listen_address.rsplit(':', 1)
cherrypy.config.update({
'server.socket_port': int(port),
'server.socket_host': host,
'tools.sessions.on': True,
'tools.sessions.storage_type': 'ram',
'tools.db.on': True,
'tools.authenticate.on': True,
'SOCIAL_AUTH_USER_MODEL': 'example.db.user.User',
'SOCIAL_AUTH_LOGIN_URL': '/',
'SOCIAL_AUTH_LOGIN_REDIRECT_URL': '/done',
})
cherrypy.config.update(SOCIAL_SETTINGS)
cherrypy.tools.jinja2env = Environment(
loader=FileSystemLoader(os.path.join(BASE_DIR, 'common', 'templates'))
)
cherrypy.tools.jinja2env.filters.update({
'backend_name': filters.backend_name,
'backend_class': filters.backend_class,
'icon_name': filters.icon_name,
'social_backends': filters.social_backends,
'legacy_backends': filters.legacy_backends,
'oauth_backends': filters.oauth_backends,
'filter_backends': filters.filter_backends,
'slice_by': filters.slice_by
})
cherrypy.tools.jinja2env.globals.update({
'url': url_for
})
cherrypy.tools.db = SATool()
cherrypy.tools.authenticate = cherrypy.Tool('before_handler', load_user)
cherrypy.tools.session = cherrypy.Tool('on_end_resource', session_commit)
cherrypy.quickstart(PSAExample())
def __init__(self):
cherrypy.Tool.__init__(self, 'on_end_request', self.record_stop)
def _setup(self):
"""Hook this tool into cherrypy.request.
The standard CherryPy request object will automatically call this
method when the tool is "turned on" in config.
"""
if appstats.get('Enabled', False):
cherrypy.Tool._setup(self)
self.record_start()
def setup_server():
def check(username, password):
# Dummy check_username_and_password function
if username != 'test' or password != 'password':
return 'Wrong login/password'
def augment_params():
# A simple tool to add some things to request.params
# This is to check to make sure that session_auth can handle
# request params (ticket #780)
cherrypy.request.params["test"] = "test"
cherrypy.tools.augment_params = cherrypy.Tool(
'before_handler', augment_params, None, priority=30)
class Test:
_cp_config = {
'tools.sessions.on': True,
'tools.session_auth.on': True,
'tools.session_auth.check_username_and_password': check,
'tools.augment_params.on': True,
}
@cherrypy.expose
def index(self, **kwargs):
return "Hi %s, you are logged in" % cherrypy.request.login
cherrypy.tree.mount(Test())