requests上传文件进度条
import os import requests class ChunkIO: def __init__(self, f, size, callback=None): self.f = f self.size = size self.callback = callback def __iter__(self): count = 0 for data in iter(self.f): count += len(data) yield data if self.callback: self.callback("\r %.2fM <-- %.2fM 进度:%d%%" % (self.size/1048576, self.size/1048576, count / self.size * 100), end=" ") def read(self, size=-1): return self.f.read(size) if __name__ == '__main__': size = os.stat(__file__).st_size with open(__file__, 'rb') as f: resp = requests.post('http://httpbin.org/post', data=ChunkIO(f, size, callback=print)) print(resp.status_code)