使用多线程通过python中的请求下载时出错

时间:2018-07-28 13:14:01

标签: python multithreading download request tqdm

我正在使用代码:

import click
import requests
import threading
from tqdm import tqdm

no_of_threads = 4

def Handler(start,end,url,filename):
    headers={'Range':'bytes=%d-%d' %(start,end)}
    r=requests.get(url,headers=headers,stream=True)
    size = int(r.headers.get('content-length', 0))
    chunk_size=1024
    with open(filename,"r+b") as fp:
        fp.seek(start)
        var=fp.tell()
        for data in tqdm(iterable=r.iter_content(chunk_size=1024), total=size / chunk_size, unit='KB'):
            fp.write(data)

@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):
    r=requests.get(url_of_file)
    r=requests.get(url_of_file)
    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) // no_of_threads
    fp = open(file_name, "wb")
    fp.write(b'\0' * file_size)
    fp.close()

    for i in range(no_of_threads):
        start = part * i
        end = start + part
        t = threading.Thread(target=Handler,
                             kwargs={'start': start, 'end': end, 'url': url_of_file, 'filename': file_name})
        t.setDaemon(True)
        t.start()

    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={})

现在我遇到两种错误:

第一:当我有网址等example.com/b/b3JnLm1pZ2h0eWZyb2cuYW5kcm9pZC5zaW1wbGVub3RlcGFkXzE2NV9hYmUwNzJhZg?_fn=U2ltcGxlIE5vdGVwYWRfdjIuMC4xX2Fwa3B1cmUuY29tLmFwaw&=b1604410dfea8938e5348408032c51ba5b5f033c&as=7b2f52eb8d502dc76cb6cb30bd946ba25b5c60b4&_p=b3JnLm1pZ2h0eWZyb2cuYW5kcm9pZC5zaW1wbGVub3RlcGFk&c=1%7CPRODUCTIVITY%7CZGV2PW1pZ2h0eWZyb2cub3JnJnZuPTIuMC4xJnZjPTE2NQ然后,

'k' is not recognized as an internal or external command,
operable program or batch file.
'as' is not recognized as an internal or external command,
operable program or batch file.
'_p' is not recognized as an internal or external command,
operable program or batch file.
'c' is not recognized as an internal or external command,
operable program or batch file.

2nd:tqdm没有显示完整的处理过程,下载完成,但是进度条显示,

1633KB [00:05, 319.09KB/s]
1633KB [00:05, 311.19KB/s]
iron.jpg downloaded███           | 991/1632.185546875 [00:04<00:02, 235.33KB/s]
 58%|████████████████▏           | 943/1632.185546875 [00:04<00:03, 223.35KB/s]

我想念什么吗?

0 个答案:

没有答案