Python uses the Bottle Lightweight Framework for Web development, pythonbottleweb

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.