Created
February 15, 2022 15:24
-
-
Save pgp/60c2a16dfa6a6b4d74b94aaa097c3bab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os.path | |
import requests | |
import tqdm | |
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor | |
def create_callback(file_size): | |
bar = tqdm.tqdm(total=file_size, unit='iB', unit_scale=True) | |
def callback(monitor): | |
bar.update(monitor.bytes_read - monitor.last_progress) | |
monitor.last_progress = monitor.bytes_read | |
return callback | |
def create_upload(file_path): | |
return MultipartEncoder({'file': (os.path.basename(file_path), open(file_path, 'rb'))}) | |
if __name__ == '__main__': | |
filepath = '/dev/shm/file.bin' | |
encoder = create_upload(filepath) | |
callback = create_callback(os.stat(filepath).st_size) | |
monitor = MultipartEncoderMonitor(encoder, callback) | |
monitor.last_progress = 0 | |
r = requests.post('https://x0.at/', data=monitor, headers={'Content-Type': monitor.content_type}) | |
print('\nUpload finished! (Returned status {0} {1})'.format(r.status_code, r.reason)) | |
download_link = r.text.strip() | |
print(download_link) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment