Can python requests retrieve url directly to a file descriptor on disk like curl?

curl has the ability to directly save file and header data to disk:

curl_setopt($curl_obj, CURLOPT_WRITEHEADER, $header_handle); curl_setopt($curl_obj, CURLOPT_FILE, $file_handle); 

Is there the same ability in python requests?

+1
source share
1 answer

As far as I know, queries do not provide a function that saves the contents to a file.

 import requests with open('local-file', 'wb') as f: r = requests.get('url', stream=True) f.writelines(r.iter_content(1024)) 

See request.Response.iter_content documentation .

iter_content (chunk_size = 1, decode_unicode = False)

Iterates according to the response. When thread = True is set to a query, this avoids reading content in memory at the same time for large responses. The chunk size is the number of bytes that it should read into memory. This is not necessarily the length of each element returned as decoding may take place.

+5
source

Source: https://habr.com/ru/post/1207631/


All Articles