Deployment¶
The bottlerun() function, when called without any parameters, starts a local development server on port 8080. You can access and test your application viahttp://localhost:8080/ if you are on the same host.
To get your application available to the outside world, specify the IP the server should listen to (e.g.run(host='192.168.0.1')) or let the server listen to all interfaces at once (e.g.run(host='0.0.0.0')). The listening port can be changed in a similar way, but you need root or admin rights to choose a port below 1024. Port 80 is the standard for HTTP servers:
# Listen to HTTP on all interfacesif__name__=='__main__':run(host='0.0.0.0',port=80)
Scaling for Production¶
The built-in development server is base onwsgiref WSGIServer, which is a very simple non-threading HTTP server implementation. This is perfectly fine for development, but may become a performance bottleneck when server load increases.
The easiest way to increase performance is to install a multi-threaded server library likecheroot orgunicorn and tell Bottle to use that instead of the single-threaded wsgiref server:
run(server='cheroot',...)# Pure Python, runs everywhererun(server='gunicorn',...)# High performance
Or using thebottle command line interface:
python3-mbottle--servergunicorn[...]mymodule:app
For production deploymentsgunicorn is a really good choice. It comes with its own command line utility that supports a lot more options than bottle. SinceBottle instances are WSGI applications, you can tellgunicorn (or any other WSGI server) to load your app instead of callingrun() yourself:
gunicorn-w4mymodule:appThis will start your application with 4 gunicorn workers and sane default settings. For more details and more complete examples, check outGunicorn Documentation.
Server adapters¶
Bottle ships with a bunch of ready-to-use adapters for the most common WSGI servers so you can try out different server backends easily. You can select a server backend viarun(server=’NAME’) orpython3 -m bottle –server NAME. Here is an incomplete list:
Name | Homepage | Description |
|---|---|---|
cgi | Run as CGI script | |
flup | Run as FastCGI process | |
gae | Helper for Google App Engine deployments | |
wsgiref | Single-threaded default server | |
cherrypy | Multi-threaded (deprectated)) | |
cheroot | Successor of cheerypy | |
paste | Multi-threaded, stable, tried and tested | |
waitress | Multi-threaded, poweres Pyramid | |
gunicorn | Pre-forked, partly written in C | |
eventlet | Asynchronous framework with WSGI support. | |
gevent | Asynchronous (greenlets) | |
diesel | Asynchronous (greenlets) | |
tornado | Asynchronous, powers some parts of Facebook | |
twisted | Asynchronous, well tested but… twisted | |
meinheld | Asynchronous, partly written in C | |
bjoern | Asynchronous, very fast and written in C | |
auto | Automatically selects the first available server adapter |
Those adapters are very basic and just for convenience, though. If you need more control over your deployment, refer to the server backend documentation and mount your Bottle application just like any other WSGI application.
WSGI Deployment¶
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 forcheroot:
importcheroot.wsgiimportmymodule.appwsgi_server=cheroot.wsgi.Server(bind_addr=('0.0.0.0',80),wsgi_app=mymodule.app)try:wsgi_server.start()finally:wsgi_server.stop()
