Python 缺少urllib响应头
我正在使用python3urllib尝试使用获取工件的URL 下面的curl工作得很好,它(正确地)显示有一个带有我想要的URL的位置头。没有响应内容:只有标题很重要:Python 缺少urllib响应头,python,python-3.x,urllib,github-api,curl,Python,Python 3.x,Urllib,Github Api,Curl,我正在使用python3urllib尝试使用获取工件的URL 下面的curl工作得很好,它(正确地)显示有一个带有我想要的URL的位置头。没有响应内容:只有标题很重要: curl-i-u“$USER:$GH\u访问令牌”https://api.github.com/repos/$ORG/$REPO/actions/artifacts/21877858/zip 对于urllib代码,auth正在工作,我可以从其他端点很好地返回JSON响应。但是我得到的标题不包括Location open
curl-i-u“$USER:$GH\u访问令牌”https://api.github.com/repos/$ORG/$REPO/actions/artifacts/21877858/zip
对于urllib代码,auth正在工作,我可以从其他端点很好地返回JSON响应。但是我得到的标题不包括Location
opener = get_opener() # bunch of boilerplate
req = request.Request(uri)
# same exact headers as curl sends.
# It doesn't change anything if I change `Accept` to "application/vnd.github.v3+json"
# like GH recommends https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#download-an-artifact
req.add_header("Accept", "*/*")
# next line doesn't make a difference
req.add_header("User-Agent", "curl/7.64")
with opener.open(req) as response:
print(response.code, response.headers["Location"])
# prints (200, None)
# If I stringify the headers there is a completely different set than
# what curl shows
但事实并非如此,因此您最终会得到一个302状态代码和一个位置
标题
urllib不会自动遵循重定向,但您可以使用,例如:
import urllib.request
githubToken = "YOUR_GITHUB_TOKEN"
url = "https://api.github.com/repos/OWNER/REPO/actions/artifacts/22063356/zip"
class NoRedirection(urllib.request.HTTPErrorProcessor):
def http_response(self, request, response):
return response
https_response = http_response
opener = urllib.request.build_opener(NoRedirection)
req = urllib.request.Request(url, None, headers = {
'Authorization' :f'Token {githubToken}'
})
with opener.open(req) as response:
print(response.code, response.headers["Location"])
输出
302 https://pipelines.actions.githubusercontent.com/NSAmkKgDUSbnHdte1rYFxmKxUVJuvcQLNt6gV7000UAFVCxMSK/_apis/pipelines/1/runs/1/signedartifactscontent?artifactName=my-artifact&urlExpires=2020-10-17T20%3A49%3A00.1948907Z&urlSigningMethod=HMACV1&urlSignature=Nq0YuQKd%2FP4jzyzGklELjfzDtBO04c7HsMgJ%2B1%2Bu%2FWY%3D