需要从服务器下载大量音频文件,并且需要记录下载日志,比如什么时候下载了什么文件,经历了多久,文件多大等,都需要记录在表中,有没有什么好的方式。

需要从服务器下载大量音频文件,并且需要记录下载日志,比如什么时候下载了什么文件,经历了多久,文件多大等,都需要记录在表中,有没有什么好的方式。

参考GPT:一下是python实现的

import requests
import logging
import time

# 配置日志格式
logging.basicConfig(filename='download.log', level=logging.INFO, format='%(asctime)s - %(message)s')

def download_file(url):
    # 获取文件名
    filename = url.split('/')[-1]
    # 发送 GET 请求,下载文件
    start_time = time.time()
    response = requests.get(url, stream=True)
    with open(filename, 'wb') as f:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
    end_time = time.time()
    # 计算下载时间和文件大小
    download_time = end_time - start_time
    file_size = int(response.headers.get('Content-Length', 0)) / 1024 / 1024
    # 记录下载日志
    logging.info(f'Downloaded {filename} ({file_size:.2f} MB) in {download_time:.2f} seconds')

# 下载文件示例
url = 'http://example.com/audio.mp3'
download_file(url)


日志并不经常需要参与查询,所以可以使用历史表,分库分表