2014-09-30 41 views
1

我正在用_cp_dispatch測試CherryPy。 但是,當我發送1個單獨帖子時,_cp_dispatch被調用兩次,而不是一次。首先爲預期的職位,然後第二次與一個get:爲什麼?Cherrypy _cp_dispatch奇怪的行爲與URL沒有結尾斜槓:POST然後GET

的代碼:

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) 

的配置文件:

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

表示cp_dispatch日誌結果被稱爲3次:1在

[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" 

與請求的交啓動和兩次郵政

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

在cherrypy中,當URL不以斜線結尾時發生內部重定向。 https://cherrypy.readthedocs.org/en/3.3.0/refman/_cprequest.html#cherrypy._cprequest.Request.is_index

有2種方法來解決 「問題」:

首先明顯張貼到http://example.com/id/12345/

二是增加了以下配置文件:

tools.trailing_slash.on = False 

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

+0

'tools.trailing_slash.on = False'就是我以前的樣子。謝謝! (上下文:我不希望信任客戶端記住結尾的斜線,尤其是從匿名用戶的瀏覽器中調用時)。 – 2015-12-05 02:17:42