python https 下载文件

同步下载

def download_file_block(url: str, file_path: str):

    logging.basicConfig(level=logging.DEBUG)
    log = logging.getLogger('requests.packages.urllib3')
    log.setLevel(logging.DEBUG)
    log.propagate = True
    class DebugAdapter(HTTPAdapter):
        def send(self, request, **kwargs):
            log.debug('Request: %s %s' % (request.method, request.url))
            for k, v in request.headers.items():
                log.debug('%s: %s' % (k, v))
            log.debug('Data: %s' % request.body)
            response = super(DebugAdapter, self).send(request, **kwargs)
            log.debug('Status code: %s' % response.status_code)
            for k, v in response.headers.items():
                log.debug('%s: %s' % (k, v))
            log.debug('Data: %s' % response.text[:200])
            return response

    with Session() as session:
        session.mount('http://', DebugAdapter())
        session.mount('https://', DebugAdapter())
        session.keep_alive = False
        print(time.strftime('%Y-%m-%d %H:%M:%S'))
        try:
            with session.get(url, verify=False, timeout=5) as response:
                response.raise_for_status()
                with open(file_path, 'wb') as file:
                    file.write(response.content)
        except requests.exceptions.RequestException as e:
            print(e)
        print(time.strftime('%Y-%m-%d %H:%M:%S'))

异步下载

async def download_file(url: str, file_path: str):
    async with httpx.AsyncClient(verify=False) as client:
        response = await client.get(url)
        response.raise_for_status()
        with open(file_path, 'wb') as file:
            file.write(response.content)
posted @ 2024-09-14 14:15  vx_guanchaoguo0  阅读(10)  评论(0编辑  收藏  举报