我是新手。 我刚刚尝试了wsgiserver的示例程序。该程序如下
from cherrypy import wsgiserver
def my_crazy_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type','text/plain')]
start_response(status, response_headers)
return ['Hello world!']
server = wsgiserver.CherryPyWSGIServer(
('127.0.0.1', 8080), my_crazy_app,
server_name='localhost')
server.start()
我成功获得了输出Hello world
,
但问题是当我点击终端上的Ctrl-c
来停止服务器时,它不会停止。怎么做?
答案 0 :(得分:1)
嗯,沿着这些方向做的事情就可以了:
import signal
from cherrypy import wsgiserver
def my_crazy_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type','text/plain')]
start_response(status, response_headers)
return ['Hello world!']
server = wsgiserver.CherryPyWSGIServer( ('127.0.0.1', 8080), my_crazy_app, server_name='localhost')
def stop_server(*args, **kwargs):
server.stop()
signal.signal(signal.SIGINT, stop_server)
server.start()