繁体   English   中英

简单的获取/发布请求在 python 3 中被阻止,但在 python 2 中没有

[英]Simple get/post request blocked in python 3 but not in python 2

我正在 python 3 中开发一个简单的 web 刮板,但是当我发送 get 或 post 请求时,响应为 403。在 python 2 中工作正常。 我在两个版本中使用相同版本的请求库。 我也尝试过使用Verify=False/True但两个版本的差异仍然存在。

请求 = 2.22.0

证书 = 2019.9.11

from requests import get
url = 'https://www.gamestop.com/'
header = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.5',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0',
    'DNT': '1',
    'Upgrade-Insecure-Requests': '1',
    'Connection': 'keep-alive',
    'Host': 'www.gamestop.com'
}
res = get(url, headers=header, verify=False).status_code
print(res)
# 403 when using python 3.7.4
# 200 when using python 2.7.16

由@blhsing 编辑:

下面的列表会根据评论跟踪哪些特定的 Python 版本有效,哪些版本失败。 到目前为止,跨平台的每个特定 Python 版本的成功和失败都是一致的。

随意使用您自己的结果以及用于生成结果的特定 Python 版本来编辑问题的这一部分。

2.7.14 works (blhsing)
2.7.16 works (repl.it)
3.6.5 works (blhsing)
3.6.8 fails (Reinderien and blhsing)
3.7.3 works (wim and blhsing)
3.7.4 fails (repl.it and blhsing)
3.8.0 fails (OP)

repl.it 上的演示: Python 2.7.16Python 3.7.4

这是 urllib3 抛出的异常:

/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/urllib3/connectionpool.py:1004:InsecureRequestWarning:正在发出未验证的 HTTPS 请求。 强烈建议添加证书验证。 请参阅: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning,

根据最新的发行说明,第 1.25.5 节(2019-09-19)

影响 Python <3.7.4 和 OpenSSL 1.1.1+ 的 BPO-37428 添加缓解措施,这会导致在使用 cert_reqs=CERT_NONE 时启用证书验证。 (问题# 1682

您可以关注Github 上的问题,该问题已关闭。

TLDR

Github 上的用户 @sethmlarson 在 urllib3 上发现了这个错误:

create_urllib3_context():

    # Enable post-handshake authentication for TLS 1.3, see GH #1634. PHA is
    # necessary for conditional client cert authentication with TLS 1.3.
    # The attribute is None for OpenSSL <= 1.1.0 or does not exist in older
    # versions of Python.
    if getattr(context, "post_handshake_auth", None) is not None:
        context.post_handshake_auth = True

将此值设置为True将启用服务器证书验证,而不是被禁用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM