Home > Backend Development > Python Tutorial > Python uses Bottle framework for web development

Python uses Bottle framework for web development

高洛峰
Release: 2017-03-03 14:07:52
Original
1761 people have browsed it

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
Copy after login

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:

Python uses Bottle framework for web development

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template