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

How to implement requests to send multipart file request in python2

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces how to achieve python2 requests to send multipart file request, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

You need to use python to send a multipart request to upload files and parameters, and there are special parameters in the request header. I searched a lot on the Internet, but I finally solved it.

Used to writing java is still a stranger to python, always using the habit of java to write python. Many api are not familiar with, and the problem is solved after all kinds of trial and error. To sum up

1. Introduction of requests

Import requests

two。 Start preparing to send the request

Requests supports various requests from resetful

Requests.get ('http://httpbin.org/get') # send get request requests.post (' http://httpbin.org/post') # send post request, simply call the post method and pass in a url parameter requests.put ('http://httpbin.org/put')requests.delete('http://httpbin.org/delete')

Try sending a simple get.

Response = requests.get ('http://www.baidu.com/')print(response.status_code)print(type(response.text))print(response.text)print(response.cookies)

Send a get request with parameters

Data = {'name':' jack', 'age': 20} resp = requests.get (' http://httpbin.org/get', params=data) print (resp.text)

Make some special settings for header

Headers= {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} resp = requests.get (' http://www.baidu.com', headers=headers) print (resp.text)

If you want to set header at the same time, pass parameters

Headers= {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} data = {' name': 'jack',' age': 20} resp = requests.get ('http://www.baidu.com', headers=headers,params=data) print (resp.text)

Try a post.

Data= {'name':' jack', 'age': 20} resp = requests.post (' http://httpbin.org/post', data=data) print (resp.text)

What if I need to upload a file?

Url = "http://httpbin.org/post"data = {..} / / generally the form value files= {...} r = requests.post (url, data, files=files)

There are many forms of files writing here, and I know two of them: dictionary type and tuple list type.

Files parameter of dictionary type

The official recommended dictionary parameter format is as follows:

Files= {"field1": ("filename1", open ("filePath2", "rb"), "field2": ("filename2", open ("filePath3", "rb"), "image/jpeg"), "field3": ("filename3", open ("filePath4", "rb"), "image/jpeg", {"refer": "localhost")} / / write a few files, at least one

The key of this dictionary is the name of the field when the post request is sent, and the value of the dictionary describes the information of the file to be sent; you can see from the above that value can be 2, 3 or 4 tuples

Each field of this tuple represents the meaning of

("filename", "fileobject", "content-type", "headers")

The default value is used by default.

In addition to the above use form, requests actually supports a more concise parameter form, as follows

{"field1": open ("filePath2", "rb"), "field2": open ("filePath3", "rb"), "field3": open ("filePath4", "rb"))}

The equivalent effect of this form of parameter is as follows, where filename is the file name of filepath:

{"field1": ("filename1", open ("filePath2", "rb"), "field2": ("filename2", open ("filePath3", "rb")), "field3": ("filename3", open ("filePath4", "rb"))}

Of course, you can also send a file request like this.

{"field1": open ("filePath2", "rb") .read ()}

The value of filename here is field1

2.2.2.The files parameter of tuple list type [("field1": open ("filePath2", "rb")), # # filename uses the file name of filepath ("field2": open ("filePath3", "rb"). Read ()) # # filename uses the key, that is, field2]

In fact, the form of the tuple list is basically the same as that of the dictionary, except that the outermost wrapper is different; and inside the requests, the dictionary parameter form will eventually be converted into the tuple column form. The recommended usage on the official website is as follows:

[("field1": ("filename1", open ("filePath2", "rb"), ["field2": ("filename2", open ("filePath3", "rb"), "image/jpeg")], ("field3": ("filename3", open ("filePath4", "rb"), "image/jpeg", {"refer": "localhost"}))]

The subitems in the list can be tuples or lists. The form of introduction is also supported here, as follows:

[("field1": open ("filePath2", "rb")), # # filename uses the file name of filepath ("field2": open ("filePath3", "rb"). Read ()) # # filename uses the key, that is, field2]

3. Send multiple files in a single field [that is, when uploading files, set to multiple selections]

Dictionary parameter form

{"field1": [("filename1", open ("filePath2", "rb")), ("filename2", open ("filePath3", "rb"), "image/png"), open ("filePath4", "rb"), open ("filePath5", "rb"). Read ()]}

3.2. Tuple list form

[("field1", ("filename1", open ("filePath2", "rb"), ("field1", ("filename2", open ("filePath3", "rb"), "image/png")), ("field1", open ("filePath4", "rb")), ("field1", open ("filePath5", "rb"). Read ()]

For the requests sent in the above two forms, all files will be in the same field, and the backend service can obtain all the file objects from the field1 field.

What if you need to set the request header? As follows

Headers= {....} data= {.} files= {....} respose=requests.post (url,data=data,headers=headers,files=files)

This basically solves my problem.

Thank you for reading this article carefully. I hope the article "how to send multipart document request in requests in python2" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Wechat

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

12
Report