I am trying to upload files using python queries. It worked in python 2.7, but not now. I am really confused and there should be a simpler answer. Since files can be quite large, I really want a progress bar, and I use presson procressbar to do the job.
r = requests.get(file_url, data={'track': 'requests'}) size = int(r.headers['Content-Length'].strip()) self.bytes = 0 widgets = [name, ": ", Bar(marker="|", left="[", right=" "), Percentage(), " ", FileTransferSpeed(), "] ", self, " of {0}MB".format(round(size / 1024 / 1024, 2))] pbar = ProgressBar(widgets=widgets, maxval=size) pbar.start() file = b"" for chunk in r.iter_content() if chunk: file += chunk self.bytes += 1 pbar.update(self.bytes)
I found that using iter_content was the best way to get continuous updates. I tried iter_lines, but it messed up the files. It stops loading suddenly and is very slow, it takes 15 minutes to load 10%, after which it stops. And an attempt to open the file in byte mode, and writing to it does not work, it does not cause an error at all. And when I try to print what the piece contains, using
print(chunk.decode("utf-8")
It works, but only a few characters. At one point he complains about
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte
Even using "decode_unicode = True" in iter_content does nothing. I'm at a standstill and don’t know what to do. It should not be difficult to use Py3k.