python wget下载

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://5ydycm.blog.51cto.com/115934/353371
一个在没有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)

本文出自 “坏男孩” 博客,请务必保留此出处http://5ydycm.blog.51cto.com/115934/353371

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值