jeudi 26 juin 2014

python requests send string as file


Vote count:

0




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.



asked 5 mins ago


1 Answer



Vote count:

0




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"
},
...
}


answered 2 mins ago





Aucun commentaire:

Enregistrer un commentaire