博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python wget下载
阅读量:4054 次
发布时间:2019-05-25

本文共 1113 字,大约阅读时间需要 3 分钟。

原创作品,允许转载,转载时请务必以超链接形式标明文章   、作者信息和本声明。否则将追究法律责任。
一个在没有wget工具情况下,写的下载工具,仅供参考!


#!/usr/bin/env python

#wget program


import sys,urllib,httplib,urlparse


def reporthook(*progress_bar_info):

    show_progress_bar_inf=progress_bar_info

    block_numbers=show_progress_bar_inf[0]

    block_size=show_progress_bar_inf[1]

    file_total_size=show_progress_bar_inf[2]

    temp_file_total_size=block_numbers*block_size

    if temp_file_total_size>file_total_size:

        print "Download Successful!"

    else:

        print str(float(temp_file_total_size)/file_total_size*100)[0:5]+"%"



def check_file_exists(url):

    host,path=urlparse.urlsplit(url)[1:3]

    if ':' in host:

        host,port=host.split(':',1)

        try:

            port=int(port)

        except ValueError:

            print 'invalid port number %r' %(port,)

            sys.exit(1) 

    else:

         port=80


    connection=httplib.HTTPConnection(host,port)

    connection.request("HEAD",path)

    resp=connection.getresponse()

    return resp.status


if __name__=='__main__':

    for url in sys.argv[1:]:

        status=check_file_exists(url)

        i=url.rfind('/')

        file=url[i+1:]

        if status==404:

            print file,"not exist!"

            sys.exit(1)

        print url,"->",file

        urllib.urlretrieve(url,file,reporthook)

本文出自 “” 博客,请务必保留此出处

你可能感兴趣的文章
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>