Python lightweight web framework: Bottle library!
Like its portability, the use of the Bottle library is also very simple. I believe that before reading this article, readers already have a simple understanding of python. So what kind of mysterious operation can complete the functions of a server with hundreds of lines of code? let us wait and see.
1. Bottle library installation
1) Use pip to install
2) Download the Bottle file
https://github.com/bottlepy/bottle/blob/master/bottle.py
2. "HelloWorld!"
The so-called success of everything Let’s start with Hello World and learn about the basic mechanism of Bottle from this simple example.
First the code:
First we import the get and run methods from the bottle library.
Next, we have to build a website. We must first have an IP address and a port. This part of the function is completed by run. In the test phase, we use 127.0.0.1 (this machine address) and port 80 (browser default port):
Run this code python HelloWorld.py
like this The website server is now running. Open the browser and enter 127.0.0.1(:80)
. The familiar 404 error message is Not found: '/' . This is of course, because in addition to the server, the website also has a very important component - the web page!
When the browser accesses the IP address, it sends a get request to the IP and waits for the web page data to be returned. Then our bottle library encapsulates the get method to implement this process.
The code is as follows:
#I don’t know if you know the @ symbol above def. This symbol is a decorator in python syntax. The meaning can be simply understood as using the get function to modify the homepage. Here, @get(‘/’) decorates the homepage into the corresponding function when the browser sends the request GET 127.0.0.1/. You can do any processing, and finally return the response to the get request. Here the simple HelloWorld page is returned. If you run it again, you will have this effect:
You can also use the template method encapsulated in bottle to separate the web page data. Written in a .tpl file, the example is as follows:
The run function also has a parameter reloader. Setting it to True will turn on automatic reloading of the web server. The server will be automatically reloaded when you make any changes, enabling hot updates of the website.
3. Dynamic routing and file download
The get('/') we used above is essentially a static routing, and the address is determined before the server runs. Routing can be done this way.
So what if it is a server runtime? For example, accessing files on the website server cannot be done in a static way. In this case, we can use dynamic routing.
Bottle's dynamic routing is implemented by the route method. Similar to get, it also uses decorators to decorate functions to implement routing functions.
#Here we see something unique appearing in the parameters of the decorator: 'name'. The parameters of the modified function have the same name as the parameter after the colon. In the function, you can use the name parameter as a processing variable, and finally return the response.
# Dynamic routing can provide convenience for file routing. There may be hundreds or thousands of files stored in a server, and it is impossible to rely on static addresses for each one.
Here we can see a new function static_file, the first parameter is the file name, the second parameter is the root directory address (that is, the location of the file), the current file The system is:
--HelloWorld.py
--store1.txt
Access the browser to get
Of course you can also put the file in a folder, just replace the root parameter with the address of the folder.
4. POST response and file upload
If we want to implement more complex functions, we not only need to use the GET method, but also the POST method. Here we use Form in HTML language to demonstrate the Bottle library's response to POST requests.
#First we implement a window for uploading files, as above.
The page here is a simple form submission interface. I will not introduce it in detail here. The page you open is as follows:
The following POST response code is as follows:
Import the post method and request from the Bottle library.
Similar to the get method, use post to decorate the response function, and then use request in the function body to obtain the post request body received by the website server. The request.forms.get() method can take out the string corresponding to the Key in the form, the request.files.get() method can take out the file corresponding to the Key in the form, and the save method can be used to store the data to achieve file uploading.
Next we conduct a test:
After clicking upload, we open the server root directory (which is the location of the python file) , check and find that the file has been uploaded successfully!
5. Summary
After completing these functions, you must want to deploy bottle to the network. After all, if it only runs locally, the website will What does it do?
Tsinghuanet provides a public IP for each of our network access points. Use ipconfig in cmd to check the IP address, change the running parameters in run to your public IP, and then Enter the IP address and port number (default 80) in the browser of any device (mobile phones are recommended, computer browsers are sometimes very slow), and you can access it!
The Bottle library also has many powerful functions, including reading and writing cookies, installing, uninstalling and disabling plug-ins. Bottle can also be deployed to other servers, making it very simple to implement multi-threading. , these functions are waiting to be explored by readers!
The above is the detailed content of Python lightweight web framework: Bottle library!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.
