简体   繁体   English

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

[英]How to launch a Bottle application over a CherryPy standalone web server?

I have a python web app developed using the bottle framework. 我有一个使用瓶子框架开发的python web应用程序。 My bottle app is web API that provide methods that return JSon data, so no static content is needed. 我的瓶子应用程序是Web API,提供返回JSon数据的方法,因此不需要静态内容。 I am trying to deploy it to production using a CherryPy server which is supposed to be robust for production applications. 我正在尝试使用CherryPy服务器将其部署到生产中,该服务器应该对生产应用程序很健壮。

My web_api.py file (my bottle app) looks something like this: 我的web_api.py文件(我的瓶子应用程序)看起来像这样:

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

I have a server.py file to launch the Bottle app over the CherryPy server that looks like this: 我有一个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()

so when I run my server using this command: 所以当我使用这个命令运行我的服务器时:

python server.py

My server is successfully started and start listening in port 80 as expected. 我的服务器已成功启动,并按预期开始在端口80中进行侦听。 However once I start my web server I cannot stop it any more. 但是,一旦我启动我的Web服务器,我就无法阻止它。 I have tried Ctrl + C which works with the development server but has no effect here. 我尝试过Ctrl + C,它与开发服务器一起使用但在这里没有效果。 Am I starting the server the right way? 我是以正确的方式启动服务器吗? How do I stop it once it is running? 一旦它运行,我该如何阻止它? Is this the correct way to launch a Bottle app over CherryPy? 这是通过CherryPy启动Bottle应用程序的正确方法吗?

BTW, I am running python 2.7 in Windows 8. 顺便说一下,我在Windows 8中运行python 2.7。

Your code is correct, you just need to add a try/catch statement: 你的代码是正确的,你只需要添加一个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()

You might wanna also consider to do some logging with wsgi-request-logger or something similar. 您可能还想考虑使用wsgi-request-logger或类似的东西进行一些日志记录

This are three alternative ways on hosting a WSGI application within cherrypy: 这是在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()

The run_in_cp_tree and run_with_adapter functions are using the cherrypy engine, which enables the use of plugins to have off-the-shelf auto-reload, pidfile, daemonization, signal management and some more goodies, along with the possibility to create one of your own. run_in_cp_treerun_with_adapter函数正在使用cherrypy引擎,它允许使用插件来进行现成的自动重载,pidfile,守护进程,信号管理和一些更好的东西,以及创建自己的插件的可能性。

Notice that you can also use the WSGIPathInfoDispatcher to attach multiple wsgi applications on the CherryPyWSGIServer. 请注意,您还可以使用WSGIPathInfoDispatcher在CherryPyWSGIServer上附加多个wsgi应用程序。

Trying to connect any WSGI server to my BottlePy app here in 2019 turned out to be rather tricky(to a noobie like me). 试图在2019年将任何WSGI服务器连接到我的BottlePy应用程序,结果相当棘手(对于像我这样的noobie)。 I tried connecting several ones, spent most off my time with CherryPy , which has changed his syntax. 我尝试连接几个,花掉了我与CherryPy的时间,这已经改变了他的语法。

The simpliest to me turned out to be waitress https://waitress.readthedocs.io/en/latest/usage.html After i figured out how to use it on waitress i got it in cherrypy also. 对我来说最简单的是服务员https://waitress.readthedocs.io/en/latest/usage.html在我想出如何在女服务员上使用它之后我也得到了它。 So: 所以:

CherryPy http://docs.cherrypy.org/en/latest/advanced.html?highlight=WSGi#host-a-foreign-wsgi-application-in-cherrypy CherryPy http://docs.cherrypy.org/en/latest/advanced.html?highlight=WSGi#host-a-foreign-wsgi-application-in-cherrypy

1) add after imports 1) 进口后添加

import cherrypy as cp

app  = bottle.Bottle()

2) change in routes "@bottle" to "@app" 2) 将路线“@bottle”更改为“@app”

3) add this as main function 3) 将其添加为主要功能

 cp.tree.graft(app, '/')
 cp.server.start()

Waitress 小姐

1) add after imports 1) 进口后添加

import waitress 

app  = bottle.Bottle()

2) add this as main function 2) 将其添加为主要功能

 waitress.serve(app, listen='*:44100')

3) change in routes "@bottle" to "@app" 3) 将路线“@bottle”更改为“@app”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM