how to launch a bottle application over a cherrypy standalone web server?

  • Last Update :
  • Techknowledgy :

Your code is correct, you just need to add a try/catch statement:

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

This are three alternative ways on hosting a WSGI application within cherrypy:

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

1)add after imports

import cherrypy as cp

app = bottle.Bottle()

3)add this as main function

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

2)add this as main function

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

Suggestion : 2

I have a server.py file to launch the Bottle anycodings_bottle app over the CherryPy server that looks like anycodings_bottle this:,This are three alternative ways on anycodings_bottle hosting a WSGI application within anycodings_bottle cherrypy:,Your code is correct, you just need to anycodings_bottle add a try/catch statement:,How to add application version to the MSI file properties?

My web_api.py file (my bottle app) looks anycodings_bottle something like this:

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 anycodings_bottle app over the CherryPy server that looks like anycodings_bottle this:

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

Your code is correct, you just need to anycodings_bottle add a try/catch statement:

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

This are three alternative ways on anycodings_bottle hosting a WSGI application within anycodings_bottle cherrypy:

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

1)add after imports

import cherrypy as cp

app = bottle.Bottle()

3)add this as main function

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

2)add this as main function

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

Suggestion : 3

I have a python web app developed using the bottle framework. My bottle app is web API that provide methods that return JSon data, so no static content is needed. I am trying to deploy it to production using a CherryPy server which is supposed to be robust for production applications. ,My server is successfully started and start listening in port 80 as expected. However once I start my web server I cannot stop it any more. I have tried Ctrl + C which works with the development server but has no effect here. 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?,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.,My web_api.py file (my bottle app) looks something like this:

My web_api.py file (my bottle app) looks something like this:

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:

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

Your code is correct, you just need to add a try/catch statement:

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

This are three alternative ways on hosting a WSGI application within cherrypy:

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

Suggestion : 4

The bottle run() function, when called without any parameters, starts a local development server on port 8080. You can access and test your application via http://localhost:8080/ if you are on the same host.,Asynchronous IO servers are very fast, can handle a virtually unlimited number of concurrent connections and are easy to manage. To take full advantage of their potential, you need to design your application accordingly and understand the concepts of the specific server.,A CGI server starts a new process for each request. This adds a lot of overhead but is sometimes the only option, especially on cheap hosting packages. The cgi server adapter does not actually start a CGI server, but transforms your bottle application into a valid CGI application:,If there is no adapter for your favorite server or if you need more control over the server setup, you may want to start the server manually. Refer to the server documentation on how to run WSGI applications. Here is an example for paste:

# Listen to HTTP requests on all interfaces
run(host = '0.0.0.0', port = 80)
run(server = 'paste')
application = bottle.default_app()
from paste
import httpserver
httpserver.serve(application, host = '0.0.0.0', port = 80)
import os
# Change working directory so relative paths(and template lookup) work again
os.chdir(os.path.dirname(__file__))

import bottle
#...build or
import your bottle application here...
   # Do NOT use bottle.run() with mod_wsgi
application = bottle.default_app()
<VirtualHost *>
    ServerName example.com

    WSGIDaemonProcess yourapp user=www-data group=www-data processes=1 threads=5
    WSGIScriptAlias / /var/www/yourapp/app.wsgi

    <Directory /var/www/yourapp>
        WSGIProcessGroup yourapp
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>
</VirtualHost>
import bottle

@bottle.route('/')
def home():
return '<html>

<head></head>

<body>Hello world!</body>

</html>'

app = bottle.default_app()