Python uses the Bottle Lightweight Framework for Web development, pythonbottleweb
Currently, Django is the most popular framework in Python Web development, but this article introduces a lightweight Web Framework: Bottle framework. Let's stop talking about the theoretical things and go directly to the instance code.
1. Problem Description
Recently, we developed a system in the background, using Python + Bottle for web Background Development. Provide an interface for the front-end to provide Json data format data through parameters when the front-end calls the interface.
2. Prepare the environment
I am using a Linux environment, python 2.7.x. Run the command: sudo pip install Bottle to install bottle. In this way, 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 * # import a bottle-related package @ route ('/helloworld/: yourword ', methods = ['get', 'post']) # url interface. Pay attention to the parameter writing format. The preceding colon indicates that the parameter def hello (yourwords): return 'Hello world. '+ yourwords # Return Foreground Data. Here, a string run (host = '0. 0.0.0 ', port = 8080) # indicates the local machine and the interface is 8080
Run python helloworld. py
Open your browser and enter: http: // 172.16.160.122: 8080/helloworld/BigData. You just need to change the IP address to your own address.
The following page is displayed:
The red circle on the image is the front-end input parameter, and the returned string content is displayed on the page.
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 at the front end, and finally the returned data is a string consisting of hello world and received parameters.
3.2 instance program code
I wanted to write the program code of an instance, but there was a program code of hello world, and nothing else was hard to understand. Understanding.
Note that all parameters received in the background are in the string format. According to your requirements, you need to perform necessary type conversion.
I hope it will help you. Thank you for reading this article.