为了账号安全,请及时绑定邮箱和手机立即绑定

捕获 SSL 问题的请求除外

捕获 SSL 问题的请求除外

侃侃尔雅 2022-05-24 16:31:16
当我发送 HEAD 请求以expired.badssl.com测试脚本将如何响应时,我收到以下错误:HTTPSConnectionPool(host='expired.badssl.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),))我可以专门捕捉到,SSLError以便我知道这是一个糟糕的 SSL 吗?我需要将此部分分配给except: CERTIFICATE_VERIFY_FAILEDfrom[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)我的请求:host = "https://expired.badssl.com/"timeout = 5try:    r = requests.head(host, headers=headers, timeout=timeout)    try:        print ('Status', r.status_code)        print ('Headers', r.headers)    except NameError:        print("Undefined variable")except requests.exceptions.Timeout:    print ('Timeout...')except requests.exceptions.TooManyRedirects:    print ('Bad URL...')except requests.exceptions.RequestException as e:    print (e)
查看完整描述

2 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

您可以通过异常参数获取它:


import requests


try:

    r = requests.head("https://expired.badssl.com/", timeout=5)


    try:

        print ('Status', r.status_code)

        print ('Headers', r.headers)


    except NameError:

        print("Undefined variable")


except requests.exceptions.SSLError as ssl_error:

    print(ssl_error)

    inner_exception = ssl_error.args[0]

    inner_ssl_error = inner_exception.reason.args[0]

    print(type(inner_ssl_error))

    for key, value in vars(inner_ssl_error).items():

        print(key, '=', value)

    # or just inner_ssl_error.reason etc.


查看完整回答
反对 回复 2022-05-24
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

测试下面的代码


import requests 


host = "https://expired.badssl.com/";

timeout = 5

try:

    r = requests.head(host)



    try:

        print ('Status', r.status_code)

        print ('Headers', r.headers)


    except NameError:

        print("Undefined variable")

except requests.exceptions.Timeout:

    print ('Timeout...')

except requests.exceptions.TooManyRedirects:

    print ('Bad URL...')

except requests.exceptions.SSLError:

    print('certificate verify failed')

except requests.exceptions.RequestException as e:

    print (e)

或者


import requests

class check_link:

  def check_broken_link(self,data):

    try:

      r = requests.head(data)                 

      try:

                print ('Status', r.status_code)

                print ('Headers', r.headers)


      except NameError:

          return ("UNDEFINED")


    except requests.exceptions.SSLError:

        return "CERTIFICATE_VERIFY_FAILED"


qc=check_link()

result =qc.check_broken_link('https://expired.badssl.com/')

print(result)


查看完整回答
反对 回复 2022-05-24
  • 2 回答
  • 0 关注
  • 103 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信