Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Python requests and django background processing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "Python requests and django background processing methods", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python requests and django background processing methods" bar!

1. Common usage of requests

In addition to url, requests also has three parameters: params, data and files, which are used to interact with the server background.

1.1. Submit query

Note that get only supports params, not data and files.

Requests.get (login_url, params= {"user": user, "password": password,}) 1.2, submit form

Note that requests.post supports both data and params:

Requests.post (form_submit_url, data= {"user": user, "password": password,}, params= {"pool": pool,}) 1.3.Adding files at the time of submission: files = ["a.csv", "b.xml"] # Note file needs to be opened in binary form. Files= [("files", (os.path.basename (file), open (file, "rb")) for file in files] r = requests.post (url, files=files, data=data, params=params) 1.4, hold status

Use session to maintain your status, log in first, and then submit the form:

Session = requests.Session () session.get (login_url, params) session.post (form_submit_url, data) 1.5, view the result

The most important are the three member variables status_code, reason, and content:

R = requests.post (url, files=files, data=data, params=params) if r.status_code! = 200: print (r.status_code, r.reason) # print error message. Else: print (r.content) # Server returns result 2, processing of django 2.1, parameters passed by params

For the parameters passed in by requests through params, you can obtain them through request.GET:

Def handler (request): return request.GET ["pool"] # if no parameter is passed, an exception will be thrown # return request.GET.get ("pool") # if the parameter is not passed, None # return request.GET.get ("pool", "abc") # defaults to the parameter passed in by abc2.2 and data

For the parameters passed in by requests through data, you can obtain them through request.POST:

Def handler (request): return request.POST ["user"] # if no parameter is passed, an exception will be thrown # return request.POST.get ("user") # if the parameter is not passed, None # return request.POST.get ("user", "abc") # defaults to the parameter passed in by abc2.3 and files

For the parameters passed in by requests through files, you can obtain them through request.FILES:

Def handler (request): for file in request.FILES.getlist ("files"): name = file.name content = file.file.read () # is binary open (name, "wb") .write (content) # saved locally, thank you for reading. This is the content of "Python requests and django background processing methods". After the study of this article I believe that you have a deeper understanding of the method of background processing of Python requests and django, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report