Python Requests Post Upload File Progress
Uploading files through Python requests module is a commonly used feature in web development. In this blog post, I will explain how to upload a file using Python requests module with progress tracking.
Using requests-toolbelt library
The requests-toolbelt is a library that provides some useful tools for the requests module, one of them being the MultipartEncoderMonitor
class. This class can be used to track the progress of multipart-encoded file uploads.
First, install the requests-toolbelt library:
!pip install requests-toolbelt
Then, use the MultipartEncoderMonitor
class to track the progress of the upload:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoderMonitor
url = 'http://localhost:5000/upload'
filepath = '/path/to/file'
def callback(monitor):
print(f'Uploaded {monitor.bytes_read} of {monitor.len} bytes')
multipart_encoder = MultipartEncoderMonitor.from_file('file', filepath, callback=callback)
headers = {'Content-Type': multipart_encoder.content_type}
response = requests.post(url, data=multipart_encoder, headers=headers)
print(response.text)
The callback
function passed to the MultipartEncoderMonitor.from_file()
method is called on every chunk of data that is uploaded. The monitor.bytes_read
attribute represents the number of bytes read so far and monitor.len
represents the total length of the file.
Using requests module with progress bar
If you don't want to use the requests-toolbelt library, you can still track the progress of the upload using a progress bar. Here's an example:
import requests
from tqdm import tqdm
url = 'http://localhost:5000/upload'
filepath = '/path/to/file'
def upload_with_progress(url, filepath):
with open(filepath, 'rb') as f:
filesize = os.path.getsize(filepath)
headers = {'Content-Length': str(filesize)}
response = requests.post(url, data=tqdm(f, total=filesize, unit='B', unit_scale=True), headers=headers)
return response
response = upload_with_progress(url, filepath)
print(response.text)
The tqdm
module is used to display a progress bar that shows the percentage of data uploaded. The unit='B'
parameter specifies that the unit of measurement is bytes and unit_scale=True
scales the units to a more readable format (e.g. KB, MB).
These are two ways to track the progress of file uploads using Python requests module. Choose the one that suits your needs the best.