2 回答
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.
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)
添加回答
举报