Python web-based static file loading instance, pythonweb

Source: Internet
Author: User

Python web-based static file loading instance, pythonweb

A very important function in web running is to load static files, which may have been set for us in django. We just need to directly load the template file.

Just put it in templates, but do you know how to load images and find the corresponding location in the basic?

Let's take a look.

In the previous article, I mentioned that the path of the static file is separately mentioned here, just to mention the role of the global variable request.

First, we will write the path of the front-end image:

 

As you can see, some people may already see it. Right, we regard the image path as the url path and parameter.

Here, when the browser sees the img tag, it will send a request to the server, so the path is equivalent:

127.0.0.1/static?file=1.gif

The Request Path is static and the parameter is file = 1.gif.

Here we have a function parse_path (),

Def parsed_path (path): ""/ss? Message = hello world & author = fei returns the following dictionary FORM {'message': 'Hello world', 'author': 'fei',} "" index = path. find ('? ') If index =-1: return path, {} else: path, query_string = path. split ('? ', 1) args = query_string.split (' & ') query ={} for arg in args: k, v = arg. split ('=') query [k] = v return path, query

After uploading a token, the filepath and 1.gif will be uploaded to the request, as shown below:

{  'file' = '1.gif', } 

In this way, we convert the parameters into a dictionary, and then

r = {   '/static': route_static,  } 

When the Request Path is static, it will switch to the route_static function. This function is written in the route, so we need to introduce it.

response = r.get(path, error) 
return response(request) 

In this way, the displayed image is switched to the route_static function.

Let's take a look at this function:

Def route_static (request): "processing function of the static resource. Read the image and generate a response." "filename = request. query. get ('file', '1.gif ') path = 'static/' + filename with open (path, 'rb') as f: header = B 'http/1.1 200 OK \ r \ nContent-Type: image/gif \ r \ n \ r \ n' img = header + f. read () return img

Let's take a look at the following sentence:

filename = request.query.get('file', 'ceshi.gif') 

In this case, filenamewill go to request's queryto find the filekey. If yes, it will go back. If no, it will go back to ceshi.gif.

Then there is the same routine, read, and then return.

In this way, a static file is placed at the specified location and read, and the process is complete.

The above python web-based loading static file instance is all the content shared by Alibaba Cloud xiaobian. I hope you can give me a reference and support the help house.

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.