如何克服Python请求包产生的412状态?

2024-09-29 23:16:51 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么使用requests包的python脚本无法下载tar.gz文件,而BASH的wget命令可以下载?它抱怨找不到请求的资源(412)。我如何克服这个问题

更新: 我发现在停用VPN后,我的python脚本可以工作。当VPN被激活时,它将不工作。但是,wget在VPN激活和未激活时工作。为什么会这样?是否需要在requests上进行一些设置以克服412状态

我的Python 3.6脚本:

#!/usr/bin/python3.6
# -*- coding: utf-8 -*-

from pathlib import Path
import requests
import sys


def download_sdk( url, sdkname ):
    print( f'\nDownloading SDK...' )
    print( f'url = {url}' )
    print( f'sdkname = {sdkname}' )
    r = requests.get( url )
    #response less than 400 response, open in binary mode and write sdk to user defined vulkan directory
    if r.ok: 
        with open( sdkname, "wb" ) as file:
            file.write( r.content )
    else:
        sys.exit( print( f'Quit: requests_status_code={r.status_code}' ) )
    print( f'\nDownload Completed.' )
    return True


sdk = f'vulkansdk-linux-x86_64-1.1.121.1.tar.gz'
url = f'https://sdk.lunarg.com/sdk/download/1.1.121.1/linux/{sdk}?u='
sdkname = Path.cwd() / sdk
download_sdk( url, sdkname )

Python脚本的输出:

Downloading SDK...
url = https://sdk.lunarg.com/sdk/download/1.1.121.1/linux/vulkansdk-linux-x86_64-1.1.121.1.tar.gz?u=
sdkname = /home/master/Vulkan/Installers/vulkansdk-linux-x86_64-1.1.121.1.tar.gz
Quit: requests_status_code=412

BASH的wget命令可以下载tar.gz文件

$ wget https://sdk.lunarg.com/sdk/download/1.1.121.1/linux/vulkansdk-linux-x86_64-1.1.121.1.tar.gz?u=
--2020-06-17 23:11:28--  https://sdk.lunarg.com/sdk/download/1.1.121.1/linux/vulkansdk-linux-x86_64-1.1.121.1.tar.gz?u=
Resolving sdk.lunarg.com (sdk.lunarg.com)... 2604:86c0:5000:3::3, 38.143.66.106
Connecting to sdk.lunarg.com (sdk.lunarg.com)|2604:86c0:5000:3::3|:443... failed: Connection refused.
Connecting to sdk.lunarg.com (sdk.lunarg.com)|38.143.66.106|:443... connected.
HTTP request sent, awaiting response... 200 OK
Syntax error in Set-Cookie: u=H0TJAB4JavYv3jKUtK1SDiu2UjlaBDHatNkvDosqN8ppYXwEVEX45Z60GLqE%0A6k5%2BerCS3KZHcD8RhV7liLcoYQ%3D%3D; ; path=/; SameSite=Lax at position 100.
Syntax error in Set-Cookie: a=XZxs35QBwvluz8x%2Bgl5n4w%3D%3D; expires=Fri, 10 Jun 2050 15:11:31 +0000; ; path=/; SameSite=Lax at position 74.
Length: 119858765 (114M) [application/octet-stream]
Saving to: ‘vulkansdk-linux-x86_64-1.1.121.1.tar.gz?u=’

vulkansdk-linux-x86_64-1.1.121.1.tar.gz?u=           100%[====================================================================================================================>] 114.31M   576KB/s    in 3m 12s  

2020-06-17 23:14:43 (610 KB/s) - ‘vulkansdk-linux-x86_64-1.1.121.1.tar.gz?u=’ saved [119858765/119858765]

Tags: 脚本comurldownloadlinuxsdktarwget
1条回答
网友
1楼 · 发布于 2024-09-29 23:16:51

我使用此函数和urllib.parse.urljoin()生成url:

import requests
from urllib.parse import urljoin

def download(url, path):
    response = requests.get(url) # get the response of the ink
    with open(path, 'wb') as file: # create the file
        file.write(response.content) # write the contents of the response to the file

base_url = 'https://sdk.lunarg.com/sdk/download/1.1.121.1/linux/'
sdk = 'vulkansdk-linux-x86_64-1.1.121.1.tar.gz'
query = '' # if you need to adda query stirng like '?u=' but that didn't make a difference in this case
url = urljoin(base_url, sdk, query) # make the full url
print(url)

download(url, f'./{sdk}')

相关问题 更多 >

    热门问题