Reputation: 1105
I am testing CherryPy with _cp_dispatch. However, when I send 1 single post, _cp_dispatch is called twice, not once. First for the expected post then a second time with a get: Why?
The code:
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)
The config file:
[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"
The post with request:
r = requests.post("http://127.0.0.1:1025/id/12345")
The log results showing that cp_dispatch is called 3 times: 1 at startup and twice for the post
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"
Any idea of why _cp_dispatch is called twice for a single post?
-- EDIT I'm suspecting some 301 redirection going on internally since it appears in the log.
Upvotes: 1
Views: 1045
Reputation: 1105
In cherrypy, an internal redirection occurs when the url does not end with a slash. https://cherrypy.readthedocs.org/en/3.3.0/refman/_cprequest.html#cherrypy._cprequest.Request.is_index
There are 2 ways to resolve the "problem":
First is obviously posting to http://example.com/id/12345/
Second is adding the following to the configuration file:
tools.trailing_slash.on = False
https://cherrypy.readthedocs.org/en/3.2.6/concepts/config.html
Upvotes: 1