跟AI学习用python制作下载器-3

python 复制代码
from http.client import responses

import requests
import threading
import os

def download_part(url,start,end,filename):
    headers = {
        "Range": f"bytes={start}-{end}"
    }

    responses = requests.get(url,headers=headers,stream=True)

    with open(filename,"wb") as f:
        f.seek(start)
        f.write(responses.content)

def multi_thread_download(url,filename,thread_num=4):

    responses = requests.head(url)
    total_size = int(responses.headers.get('content-length',0))

    print("文件大小:",total_size)

    with open(filename,"wb") as f:
        f.truncate(total_size)

    part = total_size // thread_num

    threads = []

    for i in range(thread_num):
        start = i*part

        if i==thread_num-1:
            end = total_size-1
        else:
            end = start+part-1

        t=threading.Thread(target=download_part,args=(url,start,end,filename))

        threads.append(t)
        t.start()

    for t in threads:
        t.join()

        print("多线程下载完成")

url = input("链接: ")
name = input("保存名: ")

multi_thread_download(url,name,thread_num=4)

这个版本是多线程并行下载,但是这个代码有问题,让我们一起看看

首先这个版本比上一个的多了许多东西

python 复制代码
headers = {"Range": f"bytes={start}-{end}"}

通过在请求头部假如Range,相当于告诉服务器,我不要整个文件,给我start字节到end字节的内容,就好比翻书一样,我翻书1到50页,你翻书51到100页。

这个

python 复制代码
with open(filename, "wb") as f:
    f.truncate(total_size)

f.trucate(size)会在硬盘上创建一个指定大小的空文件,为啥要用trucate呢,是因为多个线程会同时往这个文件中不同位置写数据,如果不先固定大小,文件指针会乱套。

这个

python 复制代码
t = threading.Thread(target=download_part, args=(url, start, end, filename))
threads.append(t)
t.start()

target线程要执行的任务也就是函数名,然后args是传给函数的参数。t.start()这行代码执行后,主程序不会等待下载完成而是直接冲下下一个循环。4个线程几乎同时开始工作。

这个

python 复制代码
for t in threads:
    t.join()

join()的意思是等一下,主程序运行到这里会停下,直到所有的子线程的下载任务都完成了,才会继续往下走,如果没有这行可能文件还没有下载完,程序就结束了。

这个

python 复制代码
f.seek(start)
f.write(responses.content)

f.seek()是最关键的一步,他把文件的光标移动到指定位置。比如线程A移动到0的位置写,线程B移动到500的位置写,大家各写各的最后拼成一个完整的文件。

现在得说一下问题在哪,这段代码的问题有两个

python 复制代码
with open(filename,"wb") as f:
        f.seek(start)
        f.write(responses.content)

首先就是以"wb"的方式打开文件,这里的代码是子线程使用的,如果每个子线程都wb,那么这个文件只能保留最后一个子线程写入的数据,因为wb会清空文件然后重新写。

我们可以改成这样

python 复制代码
def download_part(url,start,end,filename):
    headers = {
        "Range": f"bytes={start}-{end}"
    }

    responses = requests.get(url,headers=headers,stream=True)

    with open(filename,"rb+") as f:
        f.seek(start)
        f.write(responses.content)

可以看到我们改成了rb+,r表示我要读取现有的文件不会清空它,b处理图片等非文本文件,+这个是一个增强符号,表示在读的基础上增加写的权力。

但是这个代码还是有问题,它的问题在这里

python 复制代码
with open(filename,"rb+") as f:

f.seek(start)

f.write(responses.content)

因为responses.content会一次性的把数据全部读入内存,如果文件很大假如是4GB,我们这里又分了4个线程,那么每个线程就是1GB,那么4个线程会瞬间占用4GB的内存。所以我们要使用之前出现过的iter_content,循环写入。

可以改成这样

python 复制代码
 with open(filename,"rb+") as f:
        f.seek(start)
        for chunk in responses.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)

这里判断chunk是为了过滤保持衔接的空块。

总体代码是这样的

python 复制代码
from http.client import responses

import requests
import threading
import os

def download_part(url,start,end,filename):
    headers = {
        "Range": f"bytes={start}-{end}"
    }

    responses = requests.get(url,headers=headers,stream=True)

    with open(filename,"rb+") as f:
        f.seek(start)
        for chunk in responses.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)


def multi_thread_download(url,filename,thread_num=4):

    responses = requests.head(url)
    total_size = int(responses.headers.get('content-length',0))

    print("文件大小:",total_size)

    with open(filename,"wb") as f:
        f.truncate(total_size)

    part = total_size // thread_num

    threads = []

    for i in range(thread_num):
        start = i*part

        if i==thread_num-1:
            end = total_size-1
        else:
            end = start+part-1

        t=threading.Thread(target=download_part,args=(url,start,end,filename))

        threads.append(t)
        t.start()

    for t in threads:
        t.join()

        print("多线程下载完成")

url = input("链接: ")
name = input("保存名: ")

multi_thread_download(url,name,thread_num=4)

现在就可以使用这个代码去下载图片或者其他东西试试了

相关推荐
Java后端的Ai之路3 小时前
【Python 教程15】-Python和Web
python
冬奇Lab4 小时前
一天一个开源项目(第15篇):MapToPoster - 用代码将城市地图转换为精美的海报设计
python·开源
灰子学技术6 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
阿蒙Amon6 小时前
TypeScript学习-第10章:模块与命名空间
学习·ubuntu·typescript
AI绘画哇哒哒6 小时前
【干货收藏】深度解析AI Agent框架:设计原理+主流选型+项目实操,一站式学习指南
人工智能·学习·ai·程序员·大模型·产品经理·转行
二十雨辰6 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码6 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚6 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂6 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas1366 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript