惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 司徒正美
C
Check Point Blog
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
IT之家
IT之家
B
Blog
博客园_首页
量子位
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
J
Java Code Geeks
H
Help Net Security
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News

博客园 - ________囧丶殇

django系列 - 安装和新建项目 SQL - 基础 javascript刷新父页面 SQL - 约束 C语言(8) - 反转单向链表 C语言(7) - 数据结构之单向链表 C语言(6) - 各种排序算法的比较 C语言(5) - 选择排序 快速排序 C语言(4) - 插入排序 C语言(3) - 冒泡排序 归并排序 C语言(2) - 从指针开始 C语言(1) - 开始之前 python实践 - 抓取网页中的图片和数据 python补充(2) - 内置函数 python补充(1) python笔记(十) - 异常和文件处理 python笔记(九) - 类 part2 python笔记(八) - 类 part1 python笔记(七) - and和or
python实践 - 下载文件
________囧丶殇 · 2009-04-29 · via 博客园 - ________囧丶殇

单线程.慢慢补充扩展

你要做的就是将文件路径加入到listfile列表中,哦还有,文件下载路径,这里为d:/tmp

#coding:utf-8
import httplib
import threading
import time
import osclass download(threading.Thread):
    
def __init__ (self,threadname,downfile):
        self.filename 
= str(downfile).split('/')[-1]
        self.domainname 
= str(downfile.split('//')[-1]).split('/')[0]
        self.file 
= downfile[downfile.index(self.domainname)+len(self.domainname):]
        threading.Thread.
__init__(self,name=threadname)
    
def run(self):
        conn 
= httplib.HTTPConnection(self.domainname)
        conn.putrequest(
'GET',self.file)
        conn.putheader(
'Referer','http://'+self.domainname)
        conn.endheaders()
        rMsg 
= conn.getresponse()
        fwrite 
= open(os.path.join('d:/tmp/',self.filename),'wb+')
        
print "start downloading"
        ftmp 
= rMsg.read(1000)
        
while len(ftmp):
          fwrite.write(ftmp)
          ftmp 
= rMsg.read(1000)
        
print "Done!\b\b\n",
        fwrite.close()
        conn.close()
#建立并启动线程
listfile = [
'http://www.scriptlover.com/wish/style/spring/C0FFE51.gif',
]
for file in listfile:
  downThread 
= download("downThread",file)
  downThread.start()