2
我想根據SSL證書檢查主機名和端口。我創建了這個功能:Python SSL證書根據公用名稱檢查主機名
@staticmethod
def common_name_check(hostname, port):
try:
ctx = ssl.create_default_context()
s = ctx.wrap_socket(socket.socket(), server_hostname=hostname)
s.connect((hostname, int(port)))
cert = s.getpeercert()
ssl.match_hostname(cert, hostname)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
return False
else:
return True
我的問題是:當證書過期時,驗證失敗。但例外的是模棱兩可:
<class 'ssl.SSLError'>
我不知道,如果錯誤是由於證書過期或壞的通用名稱
我怎麼能只檢查是否主機名/端口是有效的證書?