Home Backend Development Python Tutorial Python uses Bottle framework for web development

Python uses Bottle framework for web development

Mar 03, 2017 pm 02:07 PM

Django is currently the most popular framework in Python Web development, but this article introduces a relatively lightweight Web framework: Bottle framework. I won’t go into the theoretical stuff and will go directly to the example code.

1. Problem description
I recently did the backend development of a system, using Python+Bottle for web backend development. Provide an interface to the front desk, and provide data in Json data format through the parameters when the front desk calls the interface.

2. Environment preparation
I am using the Linux environment, python 2.7.x version of python. Before using Bottle, you need to install bottle with pip. Enter the command: sudo pip install bottle to install it. In this way, you have the Bottle environment, and 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 *                             #导入bottle相关的包 
 
@route('/helloworld/:yourwords', methods=['GET', 'POST'])           #url接口,注意参数书写格式,前面有个冒号表示是参数 
def hello(yourwords):                              
  return 'hello world. ' + yourwords                  #返回前台数据,此处返回一个字符串 
 
run(host='0.0.0.0', port=8080)                        #表示本机,接口是8080
Copy after login

Run the program: python helloworld.py
Open the browser and enter: http://172.16.160.122:8080/helloworld/BigData, you just need to change the ip address to your own address.
will display the following page:

Python uses Bottle framework for web development

The part circled in red in the picture is the front desk input Parameters, the page displays the returned string content.
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 by the front desk. The final data returned is: hello world plus the string composed of the received parameters.

3.2 Example program code
I originally wanted to write an example program code, but there is the previous hello world program code, and the rest is not difficult. If you understand, I won’t write anymore. understanding.

One thing to note is that the parameters received by the background are all in string format. According to your requirements, necessary type conversion is required.

I hope it will be helpful to everyone, thank you for reading.

For more articles related to Python using the Bottle framework for web development, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles