Python单接口请求和接口自动化框架请求解决Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAIL

项目实践总结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平级。

以上就是对SSL报错的处理,希望我的经历能给你带来帮助,有用的话记得点赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值