所以我有这个奇怪的问题:
我正在熟悉 Rest-APIhttps://haveibeenpwned.com/API/v2。
我编写了一个小型 Python 脚本来检查哈希值。当我在 Anaconda 提示符中运行它时,它工作正常。在浏览器中输入查询也有效。但是当我在 PowerShell 中尝试它时,它突然崩溃了……但引发的异常对我来说没有任何意义——至少在这个特定的上下文中。如果我按照描述多次调用查询,实际上可能会出现问题这里。但就我而言,它只被调用一次。
代码如下:
import requests
import sys
print("Checking haveibeenpwnd.com for hash: \'", sys.argv[1], "\'")
query = 'https://api.pwnedpasswords.com/range/' + sys.argv[1]
print("DEBUG : URL is: ", query)
try:
req = requests.get (query)
print(req.text)
except Exception as e:
print("EXCEPTION : Something went wrong...\n")
print(str(e) + "\n")
Conda 提示符按预期工作:
d:\dev\PasswordChecker>python PasswordChecker.py FACD3
Checking haveibeenpwnd.com for password: ' FACD3 '
DEBUG : URL is: https://api.pwnedpasswords.com/range/FACD3
00286E9BB9584421CD4BE0E04C6FBC1B9A4:1
01D3C8B75DD8386F5465924A71BC27050E7:1
...
但是 PowerShell 崩溃了:
PS D:\dev\PasswordChecker> python .\PasswordChecker.py
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 588, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 248, in _get_conn
return conn or self._new_conn()
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 816, in _new_conn
raise SSLError("Can't connect to HTTPS URL because the SSL "
urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.pwnedpasswords.com', port=443): Max retries exceeded with url: /range/FAFA3 (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\PasswordChecker.py", line 8, in <module>
req = requests.get ('https://api.pwnedpasswords.com/range/FAFA3')
File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.pwnedpasswords.com', port=443): Max retries exceeded with url: /range/FAFA3 (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))
PS D:\dev\PasswordChecker> python .\PasswordChecker.py FAFC3
Checking haveibeenpwnd.com for password: ' FAFC3 '
DEBUG : URL is: https://api.pwnedpasswords.com/range/FAFC3
EXCEPTION : Something went wrong...
HTTPSConnectionPool(host='api.pwnedpasswords.com', port=443): Max retries exceeded with url: /range/FAFC3 (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))
我不介意使用 Conda,但如果知道 Python 和 PowerShell 总体上存在一些问题(以奇怪的方式表现出来),那将会相当有趣。
感谢您的支持,SH
编辑:可能值得一提 - 可能不重要:这是一台 Win10 机器。我正在使用 VS2019 进行编码。Python 是 3.7。
答案1
您在 powershell 上安装的请求库的版本可能与在 conda 中运行的版本不同,这可以解释为什么您得到不同的结果。
看起来 powershell 缺少 SSL 库。您可能需要安装它。我以为它与请求库捆绑在一起,因此重新安装请求可能对您有用:pip install requests --upgrade
。
如果您只是想摆脱错误,而并不关心验证 SSL 证书,那么您可能只想将其verify=False
作为参数传递到您的 get 请求中:
requests.get(query, verify=False)
更多相关信息请参见:http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification