नमस्ते मैं वेबसाइट से फ़ाइलों को डाउनलोड करने के लिए इस कोड स्निपेट का उपयोग कर रहा हूं, अब तक 1 जीबी से छोटी फाइलें अच्छी हैं। लेकिन मैं देखा एक 1.5GB फ़ाइल अधूरा हैअनुरोध respond.iter_content() अपूर्ण फ़ाइल (1.5 जीबी के बजाय 1024 एमबी) हो जाता है?
# s is requests session object
r = s.get(fileUrl, headers=headers, stream=True)
start_time = time.time()
with open(local_filename, 'wb') as f:
count = 1
block_size = 512
try:
total_size = int(r.headers.get('content-length'))
print 'file total size :',total_size
except TypeError:
print 'using dummy length !!!'
total_size = 10000000
for chunk in r.iter_content(chunk_size=block_size):
if chunk: # filter out keep-alive new chunks
duration = time.time() - start_time
progress_size = int(count * block_size)
if duration == 0:
duration = 0.1
speed = int(progress_size/(1024 * duration))
percent = int(count * block_size * 100/total_size)
sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" %
(percent, progress_size/(1024 * 1024), speed, duration))
f.write(chunk)
f.flush()
count += 1
centos 6.4 नवीनतम अनुरोध 2.2.1 का उपयोग कर अजगर 2.6.6, फ़ाइल डाउनलोड हमेशा 66.7% 1024MB पर रुक जाता है, मैं क्या याद आ रही है? उत्पादन:
file total size : 1581244542
...67%, 1024 MB, 5687 KB/s, 184 seconds passed
यह जनरेटर iter_content() द्वारा दिया लगता है सोचता है कि सब हिस्सा प्राप्त किए गए हैं और वहाँ कोई त्रुटि है। btw अपवाद भाग नहीं चला था, क्योंकि सर्वर ने प्रतिक्रिया शीर्षलेख में सामग्री-लंबाई वापस कर दिया था।
नोट "बी" = बिट, जबकि "बी" = बाइट (जो शायद आपका मतलब है) –
@ जोनाथन ठीक है ... ओआरजी, मैंने – Shuman
पोस्ट को अपडेट किया है 's.get (...) 'में' s' क्या है? –