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接口, note the parameter writing format, preceded by a colon that is the parameter Def Hello (yourwords): return ' Hello World '. ' + yourwords #返回前台数据, here returns a string run (host= ' 0.0.0.0 ', port=8080)
Running the program: Python helloworld.py
Open the browser, enter:http://172.16.160.122:8080/helloworld/BigData, you just change the IP address to your own address on the line
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
Originally wanted to write an instance of the program code, but there is a front Hello World program code, the other is nothing difficult to understand, do not write. Understanding.
One thing to note is that the parameters that are accepted in the background are formatted as strings, and the necessary type conversions are required, depending on your requirements.
We hope to help you, thank you for your reading.