如何修复 VS Code 环境中的“发生异常:SSLError HTTPSConnectionPool”

use*_*008 3 python windows error-handling ssl

我尝试使用 python requests 库,但收到此错误 我大部分时间在 Windows 10 中使用 psiphon VPN,并在调用后收到以下错误requests.get('[API URL]')

Exception has occurred: SSLError
HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /user (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)')))

During handling of the above exception, another exception occurred:


During handling of the above exception, another exception occurred:

  File "C:\Users\Hessam\Desktop\QWE.py", line 3, in <module>
    r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
Run Code Online (Sandbox Code Playgroud)

Ole*_*sev 9

您应该尝试添加verify=False到您的请求中:

\n
import requests\nr = requests.get('https://api.github.com/user', verify=False)\n
Run Code Online (Sandbox Code Playgroud)\n

requests验证 HTTPS 请求的 SSL 证书,就像 Web 浏览器一样。默认情况下,启用 SSL 验证,requests如果\xe2\x80\x99s 无法验证证书,则会抛出 SSLError。

\n

根据您的具体情况,您的 VPN 上的 SSL 证书很可能存在问题。

\n

请注意,当verify设置为时Falserequests将接受服务器提供的任何 TLS 证书,并忽略主机名不匹配和/或过期的证书,这将使您的应用程序容易受到中间人 (MitM) 攻击。设置verifyFalse在本地开发或测试期间可能很有用。

\n

  • 虽然此代码片段可以解决问题,但[包括解释](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而那些人可能不知道您建议代码的原因。 (2认同)