-
-
Save phith0n/7719ad04f07d6d1cd076a95ba6ce15b9 to your computer and use it in GitHub Desktop.
write tempfile with stream uploading
This file contains hidden or 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
| requests | |
| requests-toolbel |
This file contains hidden or 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 requests | |
| import time | |
| from requests_toolbelt import MultipartEncoder, StreamingIterator | |
| import argparse | |
| import os | |
| class CustomIter(object): | |
| def __init__(self, size: int, sleep: int): | |
| self.len = size | |
| self.sleep = sleep | |
| def read(self, chunk_size): | |
| if self.len > 0: | |
| time.sleep(self.sleep) | |
| read_len = min(chunk_size, self.len) | |
| print("write a chunk, chunk_size: ", chunk_size, "read_len: ", read_len) | |
| self.len -= read_len | |
| return b'a' * read_len | |
| else: | |
| return b'' | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Upload a file with custom chunk size and url.') | |
| parser.add_argument('-u', '--url', type=str, help='Upload URL') | |
| parser.add_argument('-c', '--chunk-times', type=int, default=20, help='Chunk times') | |
| parser.add_argument('-s', '--sleep', type=int, default=1, help='Sleep time in seconds') | |
| parser.add_argument('-f', '--filename', type=str, help='The real file you want to upload', required=True) | |
| args = parser.parse_args() | |
| m = MultipartEncoder( | |
| fields=( | |
| ('real', (os.path.basename(args.filename), open(args.filename, 'rb'), 'application/octet-stream')), | |
| ('fake', ('fake.txt', CustomIter(args.chunk_times*16*1024, args.sleep), 'text/plain')), | |
| ) | |
| ) | |
| response = requests.post(args.url, data=m, headers={'Content-Type': m.content_type},stream=True) | |
| print(response.text) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment