Follow + star, learn new Python skills every day
From the Internet, intrusion and deletion
Bottle is an ultra-lightweight python library. It is said to be a library, which itself is only composed of a file of about 4000 lines, and does not require any dependencies, it can operate only by the python standard library.
As light as it is, the Bottle library is very simple to use. I believe that before reading this article, readers already have a simple understanding of python. So what kind of mysterious operation can be used to complete the function of a server with a hundred lines of code? let us wait and see.
one. Bottle library installation
1) Install using pip
2) Download the Bottle file
https://github.com/bottlepy/bottle/blob/master/bottle.py
two. "Hello World!"
The so-called HelloWorld, from this simple example, understand the basic mechanism of Bottle.
Code first:
First we import the get and run methods from the bottle library.
Next, we need to build a website. First, we need an ip address and a port (port). This part of the function is done by run. In the test session, we use 127.0.0.1 (local address) and port 80 (browser). default port) to: run this code python HelloWorld.py
In this way, the website server is running, open the browser and enter 127.0.0.1(:80)
The familiar 404, the error prompts 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, in our bottle library, the get method is encapsulated to realize this process.
code show as below:
I don't know if you know the @ symbol above def. This symbol means the decorator in python syntax. It can be simply understood as using the get function to decorate the homepage. Here, @get('/') decorates the homepage as the corresponding function when the browser sends the GET 127.0.0.1/ request. You can do arbitrary processing, and finally return the response of the get request. Here, the simple HelloWorld page is returned, and if you run it again, it will have this effect:
You can also use the template method encapsulated in the bottle to write the web page data separately in the .tpl file. The example is as follows:
The run function also has a parameter reloader. Setting it to True will turn on automatic reloading. The web server will automatically reload the server when you make any changes, enabling hot updates of the website.
three. Dynamic routing and file download
The get('/') we used above is essentially a static route, and the address determined before the server runs can be routed by this method.
So what if it's a server runtime? For example, when accessing files on a website server, of course, a static method cannot be adopted. At this time, 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 an unusual occurrence in the decorator's arguments: 'name'. The parameter of the function modified by it has the same name as the parameter after the colon, and in the function, you can use the name parameter as the 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 for each of them to rely on static addresses.
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 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.
Four. POST response and file upload
If we want to implement more complex functions, we need to use not only 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, which will not be described in detail here. The open page is as follows:
The following POST response, the 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 is used to store the data to realize the upload of the file.
Next we perform a test:
After clicking upload, we open the server root directory (that is, the location of the python file) and check that the file has been uploaded successfully!
five. summary
After completing these functions, you must want to deploy the bottle to the network. After all, if it only runs locally, what is the role of the website?
Tsinghua.com provides a public IP for each of our network access points. Use ipconfig in cmd to view the IP address, change the running parameter in run to your public IP, and then use the browser of any device. (It is recommended that mobile phones, computer browsers sometimes be very slow) Enter the ip address and port number (default 80), and you can access it!
The Bottle library also has many powerful functions, such as reading and writing cookies, installing, uninstalling, and disabling plug-ins. Bottle can also be deployed to other servers, which is very simple to achieve multi-threading.
Long press or scan the QR code below to get free Python open courses and hundreds of gigabytes of learning materials packaged by the big guys , including but not limited to Python e-books, tutorials, project orders, source code, cracked software, etc.
▲ Scan the QR code - get it for free
Recommended reading
Breaking the world record! Automatic mine sweeping with Python! Artifact! A Python efficient crawler framework that is easier to use than requests! Eight schemes for implementing timed tasks in Python! Python makes progress bar, there are so many methods