在没有尾随斜杠的情况下,使用url的Cherrypy _cp_dispatch奇怪的行为:POST然后GET

时间:2014-09-30 17:19:48

标签: rest python-3.x cherrypy

我正在使用_cp_dispatch测试CherryPy。 但是,当我发送1个帖子时,_cp_dispatch被调用两次,而不是一次。首先是预期的帖子,然后是第二次获得:为什么?

代码:

import os
import cherrypy

class WebServerApp:

    def __init__(self):
        self.index_count = 0
        self.cpdispatch_count = 0

    def __del__(self):
        self.exit()

    def _cp_dispatch(self, vpath):
        self.cpdispatch_count += 1
        cherrypy.log.error('_cp_dispatch: ' + str(vpath) + ' - index count:' + str(self.cpdispatch_count))

        if len(vpath) == 0:
            return self

        if len(vpath) == 2:
            vpath.pop(0)
            cherrypy.request.params['id'] = vpath.pop(0)
            return self
        return vpath

    @cherrypy.expose
    def index(self, **params):
        try:
            self.index_count += 1
            cherrypy.log.error('Index: received params' + str(params) + ' - index count:' + str(self.index_count))
        except Exception as e:
            cherrypy.log.error(e.message)

    def exit(self):
        cherrypy.log.error('Exiting')

    exit.exposed = True

ws_conf = os.path.join(os.path.dirname(__file__), 'verybasicwebserver.conf')
if __name__ == '__main__':
    cherrypy.quickstart(WebServerApp(), config=ws_conf)

配置文件:

[global]
server.socket_host = "127.0.0.1"
server.socket_port = 1025
server.thread_pool = 10
log.screen = True
log.access_file = "/Users/antoinebrunel/src/Rankings/log/cherrypy_access.log"
log.error_file = "/Users/antoinebrunel/src/Rankings/log/cherrypy_error.log"

有要求的帖子:

r = requests.post("http://127.0.0.1:1025/id/12345")

日志结果显示cp_dispatch被调用3次:启动时为1,发布时为2次

pydev debugger: starting (pid: 5744)
[30/Sep/2014:19:16:29] ENGINE Listening for SIGUSR1.
[30/Sep/2014:19:16:29] ENGINE Listening for SIGHUP.
[30/Sep/2014:19:16:29] ENGINE Listening for SIGTERM.
[30/Sep/2014:19:16:29] ENGINE Bus STARTING
[30/Sep/2014:19:16:29]  _cp_dispatch: ['global', 'dummy.html'] - _cp_dispatch count:1
[30/Sep/2014:19:16:29] ENGINE Started monitor thread '_TimeoutMonitor'.
[30/Sep/2014:19:16:29] ENGINE Started monitor thread 'Autoreloader'.
[30/Sep/2014:19:16:29] ENGINE Serving on http://127.0.0.1:1025
[30/Sep/2014:19:16:29] ENGINE Bus STARTED
[30/Sep/2014:19:16:34]  _cp_dispatch: ['id', '12345'] - _cp_dispatch count:2
127.0.0.1 - - [30/Sep/2014:19:16:34] "POST /id/12345 HTTP/1.1" 301 117 "" "python-requests/2.4.0 CPython/3.4.1 Darwin/13.3.0"
[30/Sep/2014:19:16:34]  _cp_dispatch: ['id', '12345'] - _cp_dispatch count:3
[30/Sep/2014:19:16:34]  Index: received params{'id': '12345'} - index count:1
127.0.0.1 - - [30/Sep/2014:19:16:34] "GET /id/12345/ HTTP/1.1" 200 - "" "python-requests/2.4.0 CPython/3.4.1 Darwin/13.3.0"

为什么单个帖子会调用两次_cp_dispatch的想法?

- 编辑 我怀疑在内部进行了一些301重定向,因为它出现在日志中。

1 个答案:

答案 0 :(得分:1)

在cherrypy中,当url没有以斜杠结尾时,会发生内部重定向。 https://cherrypy.readthedocs.org/en/3.3.0/refman/_cprequest.html#cherrypy._cprequest.Request.is_index

有两种方法可以解决“问题”:

首先显然是张贴到http://example.com/id/12345/

其次是在配置文件中添加以下内容:

tools.trailing_slash.on = False

https://cherrypy.readthedocs.org/en/3.2.6/concepts/config.html