gpt4 book ai didi

python - curl -u 和 python 请求之间有区别吗

转载 作者:行者123 更新时间:2023-12-03 15:46:09 32 4
gpt4 key购买 nike

所以我想用 python.Requests 检索一个网页

https://ororo.tv/api/v2/episodes/9

这需要基本身份验证。如果我像这样 curl

 curl -u test@example.com:password https://ororo.tv/api/v2/episodes/9

然而,当我尝试在 python 中使用 Requests 做同样的事情时,我得到了我想要的响应。图书馆,像这样
>>> r = requests.get('https://ororo.tv/api/v2/episodes/9', auth=('test@example.com', 'password'))
>>> r
<Response [520]>

我总是收到 520 响应。有人能告诉我,我可能做错了什么吗?

最佳答案

是的,有细微的差别。发送的 header 略有不同,这些显然对这个 API 很重要。

如果您将查询的 URL 更改为使用 http://httpbin.org/get (在线 HTTP test service HTTPBin.org 的一个端点,你可以看到 curlrequests 发送的区别:

$ curl -u test@example.com:password http://httpbin.org/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Authorization": "Basic dGVzdEBleGFtcGxlLmNvbTpwYXNzd29yZA==",
"Host": "httpbin.org",
"User-Agent": "curl/7.51.0"
},
"origin": "84.92.98.170",
"url": "http://httpbin.org/get"
}
$ python -c "import requests; print(requests.get('http://httpbin.org/get', auth=('test@example.com', 'password')).text)"
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": "Basic dGVzdEBleGFtcGxlLmNvbTpwYXNzd29yZA==",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.11.1"
},
"origin": "84.92.98.170",
"url": "http://httpbin.org/get"
}

要突出差异:
  • requests发送一个额外的 header ,Accept-Encoding ,设置为 gzip, deflate
  • User-Agent header 不同;两者都反射(reflect)了当前的代理。

  • 您必须查看这些 header 中的哪一个导致了 https://ororo.tv/api/v2 上的问题。地点。当我更正 URL 以使用 v2 时和 https ,如 curl命令, 设置 User-Agent header 然后我得到一个有效的响应:
    >>> headers = {'User-Agent': 'curl/7.51.0'}
    >>> r = requests.get('https://ororo.tv/api/v1/episodes/9',
    auth=('test@example.com', 'password'), headers=headers)
    >>> r
    <Response [200]>
    >>> from pprint import pprint
    >>> pprint(r.json())
    {'airdate': '2005-10-13',
    'download_url': 'https://static-uk2.ororo.tv/uploads/video/file/9/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage_1480525209.mp4?attachment=true&wmsAuthSign=aWQ9ODAzNDI3Kyt2aWRlbys5JnNlcnZlcl90aW1lPTIvOC8yMDE3IDI6Mjc6MDQgUE0maGFzaF92YWx1ZT1kbEpGM3c1bldSOXBOMUg5V2N1S0NnPT0mdmFsaWRtaW51dGVzPTk2MCZzdHJtX2xlbj05NQ%3D%3D',
    'id': 9,
    'name': 'Everybody Hates Sausage',
    'number': '4',
    'plot': 'When Julius buys a big crate of sausage, he makes everyone eat it '
    'with every meal. But Tonya refuses to, causing friction between the '
    'her and Rochelle. While at school, Chris is sentenced to 3 days of '
    'detention after a rumor goes round about him beating up the school '
    'bully, Joey.',
    'resolution': 'SD',
    'season': 1,
    'show_name': 'Everybody hates Chris',
    'subtitles': [{'lang': 'en',
    'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/4867/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage.eng.vtt'},
    {'lang': 'ru',
    'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/28629/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage.vtt'},
    {'lang': 'es',
    'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/55744/1x04_EH_Sausage.vtt'},
    {'lang': 'pt',
    'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/124429/Everybody_Hates_Chris_104_-_Everybody_Hates_Sausage.vtt'},
    {'lang': 'cs',
    'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/217213/Everybody_Hates_Chris_104_-_Everybody_Hates_Sausages.vtt'},
    {'lang': 'tr',
    'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/192405/Everybody_Hates_Chris_S01E04_-_Everybody_Hates_Sausages-tur.vtt'}],
    'updated_at': 1480640069,
    'url': 'https://static-gra.ororo.tv/uploads/video/file/9/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage_1480525209.smil/playlist.m3u8?wmsAuthSign=aWQ9ODAzNDI3Kyt2aWRlbys5JnNlcnZlcl90aW1lPTIvOC8yMDE3IDI6Mjc6MDQgUE0maGFzaF92YWx1ZT1FajlGK2JPMEd3aU1Lc3lnN1M4NlpBPT0mdmFsaWRtaW51dGVzPTk2MCZzdHJtX2xlbj05Ng%3D%3D'}

    关于python - curl -u 和 python 请求之间有区别吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114632/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com