Home >Backend Development >Python Tutorial >Python lightweight web framework: Bottle library!

Python lightweight web framework: Bottle library!

WBOY
WBOYforward
2023-04-13 14:10:061307browse

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

Python lightweight web framework: Bottle library!

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:

Python lightweight web framework: Bottle library!

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

Python lightweight web framework: Bottle library!

like this The website server is now running. Open the browser and enter 127.0.0.1(:80)

Python lightweight web framework: Bottle library!

. 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:

Python lightweight web framework: Bottle library!

#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:

Python lightweight web framework: Bottle library!

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:

Python lightweight web framework: Bottle library!

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.

Python lightweight web framework: Bottle library!

#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.

Python lightweight web framework: Bottle library!

# 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.

Python lightweight web framework: Bottle library!

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

Python lightweight web framework: Bottle library!

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.

Python lightweight web framework: Bottle library!

#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:

Python lightweight web framework: Bottle library!

The following POST response code is as follows:

Python lightweight web framework: Bottle library!

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:

Python lightweight web framework: Bottle library!

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!

Python lightweight web framework: Bottle library!

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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete