Windows环境中Python SSL异常:Can't connect to HTTPS URL because the SSL module is not available

Python 调用request模块访问https网站报错: Can't connect to HTTPS URL because the SSL module is not available。

环境:Anaconda3.

安装Anaconda3,开发环境使用Anaconda3 自带的Jupyter Notebook。 相关函数代码示例如下:

 


import request

import base64

def GetJiraTickets():
    jiraticketshtml=''''''
    url="https://www.baidu.com.cn"
    auth=base64.b64encode(b'username:password')
    auth=auth.decode('utf8')
    header = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": 'Basic {}'.format(auth),
        "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"
    }

    response = requests.request("POST",url,data=None,headers=header)

    #print(response.status_code)

    return response.text.

这个函数在Anaconda的Jupyter Notebook中运行正常,为了方便调用,创建bat脚本,添加下面一行命令来执行该python 脚本:

C:\Anaconda3\python.exe C:\WorkSpace\PythonProgram\Deployment\CreateComplianceReport2.0.py 

但双击运行该bat脚本时报告如下错误:


Microsoft Windows [Version 10.0.16299.1268]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\scnzqz>C:\Anaconda3\python.exe C:\WorkSpace\PythonProgram\Deployment\CreateComplianceReport2.0.py
Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 588, in urlopen
    conn = self._get_conn(timeout=pool_timeout)
  File "C:\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 248, in _get_conn
    return conn or self._new_conn()
  File "C:\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:\Anaconda3\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\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='www.ondemand.sas.com', port=443): Max retries exceeded with url: /jira/issues/?filter=22079 (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 "C:\WorkSpace\PythonProgram\Deployment\CreateComplianceReport2.0.py", line 274, in
    sendmail()
  File "C:\WorkSpace\PythonProgram\Deployment\CreateComplianceReport2.0.py", line 268, in sendmail
    mail.HTMLBody  = CreateBodyHtml()
  File "C:\WorkSpace\PythonProgram\Deployment\CreateComplianceReport2.0.py", line 248, in CreateBodyHtml
    statushtml=CreateStatusHtml()
  File "C:\WorkSpace\PythonProgram\Deployment\CreateComplianceReport2.0.py", line 105, in CreateStatusHtml
    ssostatus['Tickets']=ssostatus['Tickets']+GetJiraTickets()
  File "C:\WorkSpace\PythonProgram\Deployment\CreateComplianceReport2.0.py", line 45, in GetJiraTickets
    response = requests.request("POST",url,data=None,headers=header)
  File "C:\Anaconda3\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Anaconda3\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Anaconda3\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\Anaconda3\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.ondemand.sas.com', port=443): Max retries exceeded with url: /jira/issues/?filter=22079 (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))

 


网上搜索大部分是Linux环境相关,有两篇帖子提到是Anaconda自带的SSL版本和系统的SSL版本不匹配导致的。很可能是系统自带的SSL版本低。下载安装最新版本的OpenSSL后问题解决。bat 脚本执行正常。

参考文章:

https://blog.csdn.net/zcyzhangzhou/article/details/86572845

https://blog.csdn.net/qq_36823518/article/details/88406237

https://blog.csdn.net/u011426236/article/details/88864469

https://blog.csdn.net/marueibo/article/details/86678936

https://slproweb.com/products/Win32OpenSSL.html

你可能感兴趣的:(Python)