介绍
下载管理器基本上是一个专用于从internet下载独立文件的计算机程序。在这里,我们将借助Python中的线程创建一个简单的下载管理器。使用多线程技术,可以从不同的线程中以块的形式同时下载文件。为了实现这一点,我们将创建一个简单的命令行工具,它接受文件的URL,然后下载它。
先决条件 安装了Python的Windows计算机。
安装程序 从命令提示符下载下面提到的包。
- 单击软件包: Click是一个Python包,用于创建漂亮的命令行界面,所需的代码非常少。这是“命令行界面创建工具包”。
pip install click
- 请求包: 在这个工具中,我们将根据URL(HTTP地址)下载一个文件。Requests是一个用Python编写的HTTP库,允许您发送HTTP请求。您可以使用简单的Python字典添加标题、表单数据、多部分文件和参数,并以相同的方式访问响应数据。
pip install requests
- 线程包: 要使用线程,我们需要线程包。
pip install threading
实施
( 注: 为了便于理解,这个程序被分成了几个部分。确保在运行代码时没有丢失代码的任何部分。)
- 在编辑器中创建新的python文件
- 首先将需要编写的包导入到
#N ote: This code will not work on online IDE # Importing the required packages import click import requests import threading # The below code is used for each chunk of file handled # by each thread for downloading the content from specified # location to storage def Handler(start, end, url, filename): # specify the starting and ending of the file headers = { 'Range' : 'bytes=%d-%d' % (start, end)} # request the specified part and get into variable r = requests.get(url, headers = headers, stream = True ) # open the file and write the content of the html page # into file. with open (filename, "r+b" ) as fp: fp.seek(start) var = fp.tell() fp.write(r.content) |
现在我们将在下载文件函数中实现的实际功能。
- 第一步是用click装饰函数。command()以便我们可以添加命令行参数。我们还可以为相应的命令提供选项。
- 对于输入command–help后的实现,我们将显示可以使用的选项。在我们的程序中,有两个选项可以使用。一个是“线程数”,另一个是“名称”。默认情况下,“线程数”为4。要更改它,我们可以在运行程序时指定。
- 提供“name”选项,以便我们可以为将要下载的文件提供自己的名称。可以使用click指定函数的参数。参数()。
- 对于我们的程序,我们需要给出我们想要下载的文件的URL。
#Note: This code will not work on online IDE @click .command( help = "It downloads the specified file with specified name" ) @click .option( '—number_of_threads' ,default = 4 , help = "No of Threads" ) @click .option( '--name' , type = click.Path(), help = "Name of the file with extension" ) @click .argument( 'url_of_file' , type = click.Path()) @click .pass_context def download_file(ctx,url_of_file,name,number_of_threads): |
下面的代码属于“下载文件”功能。
- 在这个函数中,我们首先检查“名称”。如果没有给出“名称”,则使用url中的名称。
- 下一步是连接到URL并获取内容和内容大小。
r = requests.head(url_of_file) if name: file_name = name else : file_name = url_of_file.split( '/' )[ - 1 ] try : file_size = int (r.headers[ 'content-length' ]) except : print "Invalid URL" return |
创建具有内容大小的文件
part = int (file_size) / number_of_threads fp = open (file_name, "wb" ) fp.write( ' ' * file_size) fp.close() |
现在,我们创建线程并传递处理程序函数,该函数具有以下主要功能:
for i in range (number_of_threads): start = part * i end = start + part # create a Thread with start and end locations t = threading.Thread(target = Handler, kwargs = { 'start' : start, 'end' : end, 'url' : url_of_file, 'filename' : file_name}) t.setDaemon( True ) t.start() |
最后加入线程并从main调用“download_file”函数
main_thread = threading.current_thread() for t in threading. enumerate (): if t is main_thread: continue t.join() print '%s downloaded' % file_name if __name__ = = '__main__' : download_file(obj = {}) |
我们已经完成了编码部分,现在按照下面显示的命令运行。py文件。
“python filename.py” –-help
此命令显示单击命令工具的“用法”以及该工具可以接受的选项。 下面是一个示例命令,我们尝试从URL下载一个jpg图像文件,并给出了线程的名称和数量。
最后,我们成功地使用它,这是用Python构建简单多线程下载管理器的方法之一。
本文由 拉胡尔·博亚纳帕利 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。