python - 如何通过 CherryPy 独立 Web 服务器启动 Bottle 应用程序?

标签 python cherrypy bottle

我有一个使用 bottle 框架开发的 python 网络应用程序。我的 Bottle 应用程序是 Web API,它提供返回 JSON 数据的方法,因此不需要静态内容。我正在尝试使用 CherryPy 服务器将其部署到生产环境中,该服务器对于生产应用程序来说应该是健壮的。

我的 web_api.py 文件(我的 Bottle 应用程序)看起来像这样:

from bottle import Bottle, request

app = Bottle()

@app.get('/stuff')
def do_stuff():
    '''
    Method that does stuff.
    '''
    stuff = {'data': 'some data'}
    # Return the environment info as Json data
    return stuff

我有一个 server.py 文件来通过 CherryPy 服务器启动 Bottle 应用程序,如下所示:

from my_package.web_api import app
from cherrypy.wsgiserver import CherryPyWSGIServer

server = CherryPyWSGIServer(
    ('0.0.0.0', 80),
    app,
    server_name='My_App',
    numthreads=30)

server.start()

所以当我使用这个命令运行我的服务器时:

python server.py

我的服务器已成功启动并按预期开始监听端口 80。然而,一旦我启动了我的网络服务器,我就无法再停止它了。我试过 Ctrl + C,它可以与开发服务器一起使用,但在这里没有效果。我是否以正确的方式启动服务器?它运行后如何停止它?这是通过 CherryPy 启动 Bottle 应用程序的正确方法吗?

顺便说一句,我在 Windows 8 中运行 python 2.7。

最佳答案

你的代码是正确的,你只需要添加一个try/catch语句:

from my_package.web_api import app
from cherrypy.wsgiserver import CherryPyWSGIServer

server = CherryPyWSGIServer(
    ('0.0.0.0', 80),
    app,
    server_name='My_App',
    numthreads=30)

try:
    server.start()
except KeyboardInterrupt:
    server.stop()

您可能还想考虑使用 wsgi-request-logger 进行一些日志记录或类似的东西。

这是在 cherrypy 中托管 WSGI 应用程序的三种替代方法:

import cherrypy as cp
from cherrypy.wsgiserver import CherryPyWSGIServer
from cherrypy.process.servers import ServerAdapter


from bottle import Bottle

app = Bottle()

@app.get('/stuff')
def do_stuff():
    '''
    Method that does stuff.
    '''
    stuff = {'data': 'some dataX'}
    return stuff

def run_decoupled(app, host='0.0.0.0', port=8080, **config):
    server = CherryPyWSGIServer((host, port), app, **config)
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()

def run_in_cp_tree(app, host='0.0.0.0', port=8080, **config):
    cp.tree.graft(app, '/')
    cp.config.update(config)
    cp.config.update({
        'server.socket_port': port,
        'server.socket_host': host
    })
    cp.engine.signals.subscribe() # optional
    cp.engine.start()
    cp.engine.block()

def run_with_adapter(app, host='0.0.0.0', port=8080, config=None, **kwargs):
    cp.server.unsubscribe()
    bind_addr = (host, port)
    cp.server = ServerAdapter(cp.engine,
                              CherryPyWSGIServer(bind_addr, app, **kwargs),
                              bind_addr).subscribe()
    if config:
        cp.config.update(config)
    cp.engine.signals.subscribe() # optional
    cp.engine.start()
    cp.engine.block()

run_in_cp_treerun_with_adapter 函数正在使用 cherrypy 引擎,它支持使用 plugins拥有现成的自动重新加载、pidfile、守护进程、信号管理和更多好东西,以及创建您自己的东西的可能性。

请注意,您还可以使用 WSGIPathInfoDispatcher在 CherryPyWSGIServer 上附加多个 wsgi 应用程序。

关于python - 如何通过 CherryPy 独立 Web 服务器启动 Bottle 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28307981/

相关文章:

python - 正则表达式,如何匹配第 n 次出现之前的所有内容

c++ - C++ 应用程序中的 PyQt4 插件

python - 将循环中的变量添加到公式中

python - 无法让 Bottle 在 Elastic Beanstalk 上运行

python - 获取python中单选按钮的 "id"值

Python/瓶/MongoDB : Unsupported response type: <type 'dict' >

python - 定义不起作用

python - CherryPy 用于网络托管控制面板应用程序

python - 如何知道 cherrypy 中的原始 url?

java - 从 Java 启动 CherryPy 导致没有响应答案