私の要件では、http urlからパッケージをダウンロードして、その進行状況を確認する必要があります。
私は以下のコードを書いています
import subprocess
from urllib import urlopen
class MyClass(object):
'''
classdocs
'''
def url_to_download(self):
url_for_download = "someurl"
file = "somefilename"
print "downloading with urllib"
response = urlopen(url_for_download)
CHUNK = 16 * 1024
with open(file, 'wb') as f:
while True:
chunk = response.read(CHUNK)
cmd = "ls -ltrh" + " " +file + " "+ "|"+ "awk '{print $5}'"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "the download progress is" + " " + str(output)
if not chunk:
break
f.write(chunk)
if __name__ == "__main__":
download = MyClass()
download.number_of_files()
download.url_to_download()
ご覧のとおり、基本的にLinuxコマンドを使用してダウンロードの進行状況を確認しました。コードに最小限の変更を加えるだけで、ダウンロードの進行状況の詳細を水平方向に表示できます。よろしくお願いします
関数urlretrieveにはreporthook
コールバックがあります。つまりurlretrieve
、3つの引数で呼び出す関数を渡します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加