Search code examples
pythonwebservercherrypy

Non blocking CherryPy does not receive anything


I am trying to run cherrypy with cherrypy.engine.start instead of cherrypy.quickstart. That's because I want to run cherrypy in non blocking state to start and stop a web server within my functional tests with py.test.

This works fine:

cherrypy.quickstart(WebServerTest(None), config=testconf)

The response to a curl is:

curl --head http://127.0.0.1:1026/index HTTP/1.1 200 OK
    Date: Thu, 08 Aug 2013 12:54:37 GMT
    Content-Length: 0
    Content-Type: text/html;charset=utf-8
    Server: CherryPy/3.2.2

But it's blocking the rest of the script to execute.

However this does not work:

testconf = path.join(path.dirname(__file__), 'webservertest.conf')
web_server = WebServerTest(None)
cherrypy.tree.mount(web_server, "", config=testconf)
cherrypy.engine.start()
time.sleep(60)
cherrypy.engine.stop()

The response to a curl is:

curl --head http://127.0.0.1:1026/index
    curl: (7) couldn't connect to host


Adding cherrypy.engine.block() aftet cherrypy.engine.start does not solve the problem.


So how can I make it work with cherrypy.engine.start()?


The webservertest.conf config file is:

[global]
server.socket_host = "127.0.0.1"
server.socket_port = 1026
server.thread_pool = 10

Solution

  • You also need to pass the conf to cherrypy.config.update(conf). This is for global config (including your server host and port), whereas the tree.mount call only sets config for that particular app. Read the source code of quickstart to see all the gory details.