Skip to content

Commit b7a96da

Browse files
author
libertyzhu
committed
ssl证书校验配置
1 parent 1b1b472 commit b7a96da

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

qcloud_cos/cos_client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
4444
Access_id=None, Access_key=None, Secret_id=None, Secret_key=None, Endpoint=None, IP=None, Port=None,
4545
Anonymous=None, UA=None, Proxies=None, Domain=None, ServiceDomain=None, KeepAlive=True, PoolConnections=10,
4646
PoolMaxSize=10, AllowRedirects=False, SignHost=True, EndpointCi=None, EndpointPic=None, EnableOldDomain=True, EnableInternalDomain=True, SignParams=True,
47-
AutoSwitchDomainOnRetry=False, VerifySSL=None):
47+
AutoSwitchDomainOnRetry=False, VerifySSL=None, SSLCert=None):
4848
"""初始化,保存用户的信息
4949
5050
:param Appid(string): 用户APPID.
@@ -76,7 +76,8 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
7676
:param EnableInternalDomain(bool): 是否使用内网域名访问COS
7777
:param SignParams(bool): 是否将请求参数算入签名
7878
:param AutoSwitchDomainOnRetry(bool): 重试请求时是否将myqcloud.com域名切换为tencentcos.cn
79-
:param VerifySSL(bool): 是否开启SSL证书校验
79+
:param VerifySSL(bool or string): 是否开启SSL证书校验, 或客户端CA bundle证书文件路径. 示例: True/False 或 '/path/certfile'
80+
:param SSLCert(string or tuple): 客户端SSL证书路径. 示例: '/path/client.pem' 或 ('/path/client.cert', '/path/client.key')
8081
"""
8182
self._appid = to_unicode(Appid)
8283
self._token = to_unicode(Token)
@@ -103,6 +104,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
103104
self._sign_params = SignParams
104105
self._auto_switch_domain_on_retry = AutoSwitchDomainOnRetry
105106
self._verify_ssl = VerifySSL
107+
self._ssl_cert = SSLCert
106108

107109
if self._domain is None:
108110
self._endpoint = format_endpoint(Endpoint, Region, u'cos.', EnableOldDomain, EnableInternalDomain)
@@ -382,8 +384,12 @@ def send_request(self, method, url, bucket=None, timeout=30, cos_request=True, c
382384
except Exception as ioe:
383385
file_position = None
384386
kwargs['data'] = to_bytes(kwargs['data'])
385-
if self._conf._ip is not None and self._conf._scheme == 'https' or self._conf._verify_ssl is False:
386-
kwargs['verify'] = False
387+
# 使用https访问时可设置ssl证书校验相关参数
388+
if self._conf._scheme == 'https':
389+
if self._conf._verify_ssl is not None:
390+
kwargs['verify'] = self._conf._verify_ssl
391+
if self._conf._ssl_cert is not None:
392+
kwargs['cert'] = self._conf._ssl_cert
387393
if self._conf._allow_redirects is not None:
388394
kwargs['allow_redirects'] = self._conf._allow_redirects
389395
exception_logbuf = list() # 记录每次重试的错误日志

0 commit comments

Comments
 (0)