2014-09-30 5 views
1

_cp_dispatch로 CherryPy를 테스트하고 있습니다. 그러나 단일 게시를 보낼 때 _cp_dispatch는 한 번이 아니라 두 번 호출됩니다. 먼저 예상 게시물에 대한 두 번째 시간을 얻을 : 왜? 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) 

config 파일 : 그 cp_dispatch을 나타내는 로그의 결과가 3 회라고

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

: 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가 두 번 호출되는 이유는 무엇입니까?

- EDIT 로그에 나타난 이후 내부적으로 진행되는 301 리디렉션이 의심됩니다.

답변

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

+0

'tools.trailing_slash.on = False'는 내가했던 것입니다. 감사! (컨텍스트 : 특히 익명 사용자의 브라우저에서 호출 할 때 후행 슬래시를 기억하도록 클라이언트를 신뢰하지 않아도됩니다. –