項(xiàng)目實(shí)踐總結(jié)2種處理SSLError的辦法,下面就詳細(xì)說明一下
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()
導(dǎo)入urllib3,導(dǎo)包下面寫上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
接口請求數(shù)據(jù),存放在yaml文件中。在接口請求體中增加verify: false字段,和headers平級。
封裝的接口請求如下:
def run(self, method, url, **kwargs):
# 執(zhí)行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對應(yīng)body里面的字段信息。
注意:在封裝接口請求的.py文件中需要導(dǎo)入urllib3,導(dǎo)包下面寫上urllib3.disable_warnings()。
在處理SSL報錯時,遇到過坑,經(jīng)歷過報錯: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平級。文章來源:http://www.zghlxwxcb.cn/news/detail-475961.html
以上就是對SSL報錯的處理,希望我的經(jīng)歷能給你帶來幫助,有用的話記得點(diǎn)贊文章來源地址http://www.zghlxwxcb.cn/news/detail-475961.html
到了這里,關(guān)于Python單接口請求和接口自動化框架請求解決Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAIL的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!