CherryPy:如何在请求到达应用程序方法之前对其进行处理? [英] CherryPy: How to process a request before it has reached the application method?
问题描述
我希望能够在方法本身之前捕获CherryPy应用程序的方法的参数.但是我不确定在CherryPy或标准python中是否有办法做到这一点.它应该看起来像这样:
I want to be able to catch the arguments of the methods of my CherryPy application before the method itself. But I'm not sure if there is a way to do it in CherryPy or with standard python. It should look something like this:
HTTP请求->解析器以捕获参数->将请求传递给方法的CherryPy
HTTP Request --> Parser to catch the arguments --> CherryPy who passes the request to method
我的目标是在不干扰方法本身代码的情况下捕获输入和输出到服务器.
My goal is to capture the input and output to a server without disturbing the code in the method itself.
另外,如何将请求重定向到CherryPy服务器到其他CherryPy服务器?
Also, how can you redirect the request to a CherryPy server to other CherryPy servers?
推荐答案
这是我检查发布方法以获取服务器生成的有效csrf令牌的方法.
Here is how I check post methods for a valid csrf token our server generates.
def check_token(self=None):
# whenever a user posts a form we verify that the csrf token is valid.
if cherrypy.request.method == 'POST':
token = cherrypy.session.get('_csrf_token')
if token is None or cherrypy.request.params.get('csrf_token') == None or token != cherrypy.request.params['csrf_token']:
raise cherrypy.HTTPError(403)
cherrypy.tools.Functions = cherrypy.Tool('before_handler', check_token)
希望这会有所帮助!
这篇关于CherryPy:如何在请求到达应用程序方法之前对其进行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!