2 回答

TA貢獻1829條經(jīng)驗 獲得超9個贊
您可以通過異常參數(shù)獲取它:
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條經(jīng)驗 獲得超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)
添加回答
舉報