How to upload files using Python's requests library?
P粉071559609
P粉071559609 2023-08-16 18:44:37
0
2
495

I am using Python's requests library to perform a simple task, which is to upload a file. I've searched on Stack Overflow and no one seems to be having the same problem, where the file is not being received by the server:

import requests url='http://nesssi.cacr.caltech.edu/cgi-bin/getmulticonedb_release2.cgi/post' files={'files': open('file.txt','rb')} values={'upload_file' : 'file.txt' , 'DB':'photcat' , 'OUT':'csv' , 'SHORT':'short'} r=requests.post(url,files=files,data=values)

I filled in the value of the 'upload_file' keyword with my filename because if I left it blank it would read:

Error - You must select a file to upload! 

Now I get:

The size of file file.txt is bytes, uploaded successfully! Query service results: There are 0 rows in total. 

This only occurs if the file is empty. So I don't know how to send my file successfully. I know the file is valid because if I fill out the form manually and visit the site, it returns a nice list of matches, which is exactly what I want. I really appreciate all the tips.

Some other threads that are related (but don't solve my problem):

  • Use a Python script to send files via POST
  • http://docs.python-requests.org/en/latest/user/quickstart/#response-content
  • Use requests to upload files and send additional data
  • http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow


P粉071559609
P粉071559609

reply all (2)
P粉422227023

(2018) The new Python requests library simplifies this process. We can use the 'files' variable to indicate that we want to upload a multi-part encoded file

url = 'http://httpbin.org/post' files = {'file': open('report.xls', 'rb')} r = requests.post(url, files=files) r.text
    P粉590428357

    Ifupload_filerefers to a file, use:

    files = {'upload_file': open('file.txt','rb')} values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'} r = requests.post(url, files=files, data=values)

    Thenrequestswill send a multipart form POST request body with theupload_filefield set to the contents of thefile.txtfile.

    The file name will be included in the mime header of the specific field:

    >>> import requests >>> open('file.txt', 'wb') # 创建一个空的演示文件 <_io.BufferedWriter name='file.txt'> >>> files = {'upload_file': open('file.txt', 'rb')} >>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii')) --c226ce13d09842658ffbd31e0563c6bd Content-Disposition: form-data; name="upload_file"; filename="file.txt" --c226ce13d09842658ffbd31e0563c6bd--

    Please note thefilename="file.txt"parameter.

    If you need more control, you can use tuples asfilesmapping values. The length of the tuple should be between 2 and 4. The first element is the file name, followed by the content, optionally including a mapping of the content-type header and other headers:

    files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}

    This will set an alternative filename and content type, omitting the optional headers.

    If you want the entire POST request body to come from a file (no other fields are specified), do not use thefilesparameter and POST the file directly asdata. You may also want to set aContent-Typeheader, otherwise no header will be set. SeePython requests - POST data from a file.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!