The framework of current Python web development is the most popular, but this article describes a more lightweight web framework: Bottle framework. The theoretical thing is not spoken, directly on the instance code.
1. Description of the problem
Recently do a system of background development, with the python+bottle for the web background development. Provides an interface to the foreground, which provides data in JSON data format through the parameters of the foreground call interface.
2. Environmental preparedness
I am using the Linux environment, Python 2.7.x version of Python. PIP installation bottle required before using bottle, enter command: sudo pip install bottle can be installed. This has the bottle environment, you can use the bottle framework for Python web development.
3. Program Code
3.1 A Hello World program
Program Files: 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 #返回前台数据, here Returns a string run (host= ' 0.0.0.0 ', port=8080) #表示本机, interface is 8080
Open Browser, type: Http://172.16.160.122:8080/helloworld/BigData
The following page is displayed:
The red circled part of the figure is the foreground input parameter, and the page displays the returned string contents.
This completes a simple example. The bottle frame is not very lightweight.
This program is very simple, the URL yourwords is the foreground input parameters, the last returned to the data is: Hello World plus the parameters of the received string.
3.2 Instance Program code
Precautions
Python python uses the bottle Lightweight framework for Web development