This article mainly introduces Python's use of the Bottle lightweight framework for Web development. if you need it, you can refer to the current Python Web development framework to calculate Django as the most popular one, however, this article introduces a lightweight Web framework: the 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:
For more articles about using the Bottle framework for Web development in Python, refer to the PHP Chinese website!