Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 捕获SSL问题的请求除外_Python_Python 3.x - Fatal编程技术网

Python 捕获SSL问题的请求除外

Python 捕获SSL问题的请求除外,python,python-3.x,Python,Python 3.x,当我向expired.badsl.com发送HEAD请求以测试脚本将如何响应时,我得到以下错误: 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)'),))

当我向
expired.badsl.com
发送HEAD请求以测试脚本将如何响应时,我得到以下错误:

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

我需要将此部分分配给
中的变量,除了
证书验证失败
来自
[SSL:CERTIFICATE\u VERIFY\u FAILED]证书验证失败(\u SSL.c:852)

我的请求:

host = "https://expired.badssl.com/"
timeout = 5

try:
    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)
测试下面的代码

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)


您可以通过exceptions参数获得它:

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.

你试过捕捉
请求。异常。SSLError
?@EdWard我试过了。它返回相同的:
HTTPSConnectionPool(host='reversed.badsl.com',port=443):url:/(由SSLError(SSLError(1,[SSL:CERTIFICATE\u VERIFY\u FAILED]CERTIFICATE VERIFY FAILED(\u SSL.c:852)]引起的最大重试次数)
这是什么意思?我需要将此部分分配给[SSL:CERTIFICATE\u VERIFY\u FAILED]中的Exception:CERTIFICATE\u VERIFY\u FAILED[SSL:CERTIFICATE\u VERIFY\u FAILED]中的变量CERTIFICATE VERIFY FAILED(_SSL.c:852)@Lifeiscomplex中的
e
我想提取SSL失败的实际原因。特别是:
证书\u验证\u失败
DH\u密钥太小
或任何其他错误。它就在这里
[SSL:DH\u KEY\u TOO\u SMALL]
。对。但是我需要它实际返回的错误,比如:
CERTIFICATE\u VERIFY\u FAILED
from
[SSL:CERTIFICATE\u VERIFY\u FAILED]CERTIFICATE VERIFY FAILED(\u SSL.c:852)
print(e.args[0])中的
DH KEY\u-TOO-SMALL
中提取
DH-KEY\u-KEY\u-SMALL
@Borsn编辑了我的答案,它
DH\u KEY\u太小了
应该在
internal\u ssl\u错误中。原因
谢谢发布此消息。我计划用这个答案来改进我自己的异常处理代码。@ChristopherKrause它就在
reason
里面。但是,我需要从
[SSL:DH\u KEY\u TOO\u SMALL]DH KEY TOO\u SMALL(\u SSL.c:852)
@Borsn中提取
[SSL:CERTIFICATE\u VERIFY\u FAILED]证书验证失败:证书已过期(\u SSL.c:1076)
作为错误,对我来说
内部\u ssl\u错误。原因
返回
“证书验证\u失败”
。你试过我的更新代码了吗?
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.