Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python requests send string as file

In my code I am currently chunking up a file and sending reading it into a temporary file, then passing this temporary file into requests. Is there a way to still send this

with open(full_path, 'r+b') as f:
  i=0
  while True:
  chunk = f.read(max_chunk_size)
  if not chunk:
    break
  with tempfile.TemporaryFile() as t:
    t.write(chunk)
    t.seek(0)
    r = requests.post(endpoint + 'upload_chunk',
                      files={'chunk':t},
                      data={'mod_time':file_update_time,
                            'directory':'/'.join(subdirs),
                            'filename':filename,
                            'chunk_pos':i},
                      auth=auth)
    i+=max_chunk_size

Is there a way to send the chunk to the server without writing it to a temporary file, then having something in requests.post read this file? I'd much prefer to not have to change the server-side code. I'd imagine not having to read/write an extra 4 megabytes would increase execution speed.

Chunking up the file is necessary; this code is not yet complete.

like image 484
Thaddeus Hughes Avatar asked Jun 27 '14 03:06

Thaddeus Hughes


1 Answers

Have a read through the Requests Quickstart page, under the POST a Multipart-Encoded File section.

There you'll find this:

If you want, you can send strings to be received as files:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}

>>> r = requests.post(url, files=files)
>>> r.text
{
  ...
  "files": {
    "file": "some,data,to,send\\nanother,row,to,send\\n"
  },
  ...
}

Note that "file" is the name of the file upload field the server is expecting. It would correspond to the following HTML:

<input type="file" name="file">
like image 90
Jonathon Reinhart Avatar answered Oct 05 '22 22:10

Jonathon Reinhart