项目实践总结2种处理SSLError的办法,下面就详细说明一下
1、单接口请求解决Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)’))报错办法
接口请求示例如下:
import requests
import urllib3
import time
from Pick.logic.Request import Request
urllib3.disable_warnings()
def tes_001():
response = requests.request(method='GET',
url=f'https://xxx/open/pick/codeId?signMode=1&clientId=af901bbe8b9f0f3eebc3db46aba8d209&signTimestamp=signTime&signData=signData',
params={"number": "66666666669995808", "shop": "06"},
verify=False,
json={"user": "135", "pass": "xx", "tag": "crv",
"locale": "CN"},
headers={"Content-Type": "text/plain"})
print(response.json())
tes_001()
导入urllib3,导包下面写上urllib3.disable_warnings(),在请求中加上verify=False。
2、接口自动化框架请求解决Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)’))报错办法
data:
- request_data:
method: GET
url: >-
https://xxx/open/pick/codeId?signMode=1&clientId=af901bbe8b9f0f3eebc3db46aba8d209&signTimestamp=signTime&signData=signData
body:
params:
number: $Id
shop: 06
json:
user: ‘135’
pass: ‘xx’
tag: crv
locale: CN
headers:
Content-Type: text/plain
verify: false
接口请求数据,存放在yaml文件中。在接口请求体中增加verify: false字段,和headers平级。
封装的接口请求如下:
def run(self, method, url, **kwargs):
# 执行post/get接口请求
self.d = kwargs
self.d = json.loads(Template(json.dumps(self.d)).safe_substitute(**self.__argument))
url = json.loads(Template(json.dumps(url)).safe_substitute(**self.__argument))
self.respond = self.r(method=method, url=url, **self.d)
# print(self.respond)
return url, method, self.d, str(self.respond.status_code)
kwargs对应body里面的字段信息。
注意:在封装接口请求的.py文件中需要导入urllib3,导包下面写上urllib3.disable_warnings()。
在处理SSL报错时,遇到过坑,经历过报错:requests.exceptions.InvalidHeader: Header part (False) from {‘verify’: False} must be of type str or bytes, not <class ‘bool’>
原因是:将verify: false字段写到了请求头headers里面,正确的写法是verify: false字段,和headers平级。文章来源:https://www.toymoban.com/news/detail-475961.html
以上就是对SSL报错的处理,希望我的经历能给你带来帮助,有用的话记得点赞文章来源地址https://www.toymoban.com/news/detail-475961.html
到了这里,关于Python单接口请求和接口自动化框架请求解决Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAIL的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!