python学习(七)
得到不重复的随机数字的方法,如下所示,建立一个列表,将需要生成的随机数字放在列表中,然后随机选出一个,同时将选出的数字移出列表:
lst = ['0','1','2','3','4','5','6','7','8','9'] value = random.choice(list(lst)) r = int(value) lst.remove(value)
多线程下载
先来学习一下多线程的创建方式,我们创建threading.Thread的子类来包装一个线程对象
1 #创建threading.Thread的子类来包装一个线程对象 2 #coding:utf-8 3 import threading 4 import time 5 6 class timer(threading.Thread): 7 def __init__(self, num, interval): 8 threading.Thread.__init__(self) 9 self.thread_num = num 10 self.interval = interval 11 self.thread_stop = False 12 13 #Overwrite run() method, put what you want the thread do here 14 def run(self): 15 while not self.thread_stop: 16 print 'Thread object(%d), Time:%s\n' %(self.thread_num, time.ctime()) 17 time.sleep(self.interval) 18 def stop(self): 19 self.thread_stop = True 20 21 def test(): 22 thread_one = timer(0,1) 23 thread_two = timer(2,4) 24 thread_one.start() 25 thread_two.start() 26 time.sleep(5) 27 thread_one.stop() 28 thread_two.stop() 29 return 30 31 if __name__ == '__main__': 32 test()
多线程下载
我们把需要下载的文件分成10部分,每部分起一个线程进行下载,具体代码如下:
1 #coding:utf-8 2 import urllib2,random 3 import threading,time 4 import sys 5 6 #创建线程 7 class newthread(threading.Thread): 8 def __init__(self, startp, end, request): 9 threading.Thread.__init__(self) 10 self.startp = startp 11 self.end = end 12 self.request = request 13 def run(self): 14 #注意使用类的变量时加上self 15 self.request.headers['Range'] = 'bytes=%s-%s' %(self.startp, self.end) 16 f = urllib2.urlopen(self.request) 17 content = f.read() 18 write_con(content, self.startp) 19 20 #写入本地文件 21 def write_con(content, position): 22 f = open("a.html","r+") 23 f.seek(position) 24 f.write(content) 25 f.close() 26 27 #获取下载的文件长度 28 def get_file_size(request): 29 #request.add_header('Accept_encoding','gzip') 30 31 f = urllib2.urlopen(request) 32 #print f.headers.values() 33 con_len = f.headers['content-length'] 34 return con_len 35 36 #分割文件进行随机下载 37 def multi_download(con_len, request): 38 i = 0 39 #获取文件分块大小 40 j = int(con_len) / 10 41 #获取无重复随机数 42 lst = ['0','1','2','3','4','5','6','7','8','9'] 43 while i < 10: 44 value = random.choice(list(lst)) 45 r = int(value) 46 print r 47 start = r*j 48 end = (r+1) * j-1 49 if r == 9: 50 end = con_len 51 nth = newthread(start, end, request) 52 nth.start() 53 54 i = i+1 55 lst.remove(value) 56 57 #读取文件 58 def read_html(): 59 f = open('a.html', 'r+') 60 print f.read() 61 f.close() 62 63 if __name__ == '__main__': 64 #获取下载链接 65 url = sys.argv[1] 66 handler = urllib2.HTTPHandler() 67 request = urllib2.Request(url) 68 #获取文件大小 69 con_len = get_file_size(request) 70 #多线程下载 71 multi_download(con_len, request) 72 #读取文件 73 read_html()