Django is currently the most popular framework in Python Web development, but this article introduces a relatively lightweight Web framework: Bottle framework. I won’t go into the theoretical stuff and will go directly to the example code.
1. Problem description
I recently did the backend development of a system, using Python+Bottle for web backend development. Provide an interface to the front desk, and provide data in Json data format through the parameters when the front desk calls the interface.
2. Environment preparation
I am using the Linux environment, python 2.7.x version of python. Before using Bottle, you need to install bottle with pip. Enter the command: sudo pip install bottle to install it. In this way, you have the Bottle environment, and you can use the Bottle framework for Python Web development.
3. Program code
3.1 A Hello World program
Program file: helloworld.py
#!/usr/bin/python # -*- conding:utf-8 -*- from bottle import * #导入bottle相关的包 @route('/helloworld/:yourwords', methods=['GET', 'POST']) #url接口,注意参数书写格式,前面有个冒号表示是参数 def hello(yourwords): return 'hello world. ' + yourwords #返回前台数据,此处返回一个字符串 run(host='0.0.0.0', port=8080) #表示本机,接口是8080
Run the program: python helloworld.py
Open the browser and enter: http://172.16.160.122:8080/helloworld/BigData, you just need to change the ip address to your own address.
will display the following page:
The part circled in red in the picture is the front desk input Parameters, the page displays the returned string content.
This completes a simple example. The Bottle framework is not very lightweight.
This program is very simple. Yourwords in the URL is the parameter input by the front desk. The final data returned is: hello world plus the string composed of the received parameters.
3.2 Example program code
I originally wanted to write an example program code, but there is the previous hello world program code, and the rest is not difficult. If you understand, I won’t write anymore. understanding.
One thing to note is that the parameters received by the background are all in string format. According to your requirements, necessary type conversion is required.
I hope it will be helpful to everyone, thank you for reading.
For more articles related to Python using the Bottle framework for web development, please pay attention to the PHP Chinese website!