4

我正在尝试使用cherrypy.engine.start 而不是cherrypy.quickstart 运行cherrypy。那是因为我想在非阻塞状态下运行cherrypy,以使用py.test在我的功能测试中启动和停止Web服务器。

这工作正常:

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

对卷曲的响应是:

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

但它阻止了脚本的其余部分执行。

但是,这不起作用:

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()

对卷曲的响应是:

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


在cherrypy.engine.start 之后添加cherrypy.engine.block() 并不能解决问题。


那么我怎样才能使它与cherrypy.engine.start()一起工作?


webservertest.conf 配置文件是:

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

1 回答 1

2

您还需要将 conf 传递给cherrypy.config.update(conf). 这是用于全局配置(包括您的服务器主机和端口),而 tree.mount 调用仅设置该特定应用程序的配置。阅读源代码quickstart以查看所有血淋淋的细节。

于 2013-08-09T06:34:01.053 回答