【问题标题】:Cherrypy _cp_dispatch strange behaviour with url without trailing slash: POST then GETCherrypy _cp_dispatch 带有不带斜杠的 url 的奇怪行为:POST 然后 GET
【发布时间】:2014-09-30 17:19:47
【问题描述】:

我正在使用 _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 次,post 时调用 2 次​​p>

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 重定向出现在内部,因为它出现在日志中。

【问题讨论】:

    标签: rest python-3.x cherrypy


    【解决方案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

    【讨论】:

    • tools.trailing_slash.on = False 是我所追求的。谢谢! (上下文:我不想让客户端记住斜杠,尤其是从匿名用户的浏览器调用时)。
    猜你喜欢
    • 2018-07-28
    • 2019-04-09
    • 2022-08-11
    • 2015-03-24
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多