Home > Article > Backend Development > Web development framework Bottle in Python
Bottle is a lightweight Python web development framework. It has a routing-based request dispatcher, integrates a WSGI server, comes with a template engine and has the ability to convert Python data types to JSON, etc. Bottle is very simple to use and is especially suitable for small projects, API development and rapid prototyping. The following will introduce Bottle from its characteristics, installation, use, deployment and other aspects.
1. Features of Bottle
Bottle is a framework that can be used after registration. The size of a single file is only a few hundred K . Bottle completely relies on the Python standard library and does not require the installation of other third-party libraries.
Bottle provides different HTTP methods through decorators, such as get(), post(), put() and delete() wait. We only need to combine these methods with URL paths to easily write a web application with RESTful API functionality.
Bottle provides a built-in WSGI server, using a single-threaded model, suitable for rapid development and testing. It can listen on multiple addresses and ports and supports IPv6.
Bottle's own template engine can easily fill data into HTML templates, supports a variety of templates, and is easy to use.
Bottle also has many other functions, such as: obtaining data sent by the client, processing of Cookies, Session support, etc.
2. Bottle installation
Bottle can be installed through pip.
pip install bottle
3. The use of Bottle
Let’s use a small example to demonstrate the use of Bottle:
from bottle import route, run @route('/') def index(): return 'Hello World!' if __name__ == '__main__': run(host='localhost', port=8080, debug=True)
After running this code, open the browser and click on the address bar Enter http://localhost:8080
, and you will see "Hello World!".
4. Deployment of Bottle
Bottle can be deployed using uWSGI or Gunicorn. Here we take uWSGI as an example.
You can install uWSGI through pip.
pip install uwsgi
Create uwsgi.ini file and add the following code:
[uwsgi] socket = 127.0.0.1:8080 chdir = /path/to/project wsgi-file = app.py callable = app processes = 4 threads = 2 stats = 127.0.0.1:9191
Parameter explanation:
Run the following command to start uWSGI:
uwsgi --ini uwsgi.ini
At this time, you can enter http://127.0. 0.1:8080 came to access our application.
In short, Bottle is a Python web development framework that is very suitable for small projects, API development and rapid prototyping. It is simple and easy to use, with only one file. The entire framework only relies on Python's standard library and does not require the installation of other third-party libraries. If you are interested in lightweight web frameworks, Bottle is worth a try.
The above is the detailed content of Web development framework Bottle in Python. For more information, please follow other related articles on the PHP Chinese website!