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.
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With