最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python獲取域名ssl證書信息和到期時(shí)間

 更新時(shí)間:2023年09月17日 08:45:47   作者:Python技術(shù)  
這篇文章主要為大家詳細(xì)介紹了如何利用python實(shí)現(xiàn)獲取域名ssl證書信息和到期時(shí)間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下

文檔

https://docs.python.org/zh-cn/3/library/socket.html

https://docs.python.org/zh-cn/3/library/ssl.html

1、通過openssl證書獲取

openssl x509 -in <cert>.pem -noout -dates

2、通過openssl域名獲取

echo | openssl s_client -servername <doman> -connect <doman>:443 2>/dev/null | openssl x509 -noout -dates

3、通過腳本獲取curl

#?coding:?utf-8?
#?查詢域名證書到期情況
import?re
import?subprocess
from?datetime?import?datetime
def?get_re_match_result(pattern,?string):
????match?=?re.search(pattern,?string)
????return?match.group(1)
def?parse_time(date_str):
????return?datetime.strptime(date_str,?"%b?%d?%H:%M:%S?%Y?GMT")
def?format_time(date_time):
????return?datetime.strftime(date_time,?"%Y-%m-%d?%H:%M:%S")
def?get_cert_info(domain):
????"""獲取證書信息"""
????cmd?=?f"curl?-Ivs?https://{domain}?--connect-timeout?10"
????exitcode,?output?=?subprocess.getstatusoutput(cmd)
????#?正則匹配
????start_date?=?get_re_match_result('start?date:?(.*)',?output)
????expire_date?=?get_re_match_result('expire?date:?(.*)',?output)
????#?解析匹配結(jié)果
????start_date?=?parse_time(start_date)
????expire_date?=?parse_time(expire_date)
????return?{
????????'start_date':?start_date,
????????'expire_date':?expire_date
????}
def?get_cert_expire_date(domain):
????"""獲取證書剩余時(shí)間"""
????info?=?get_cert_info(domain)
????print(info)
????expire_date?=?info['expire_date']
????#?剩余天數(shù)
????return?(expire_date?-?datetime.now()).days
if?__name__?==?"__main__":
????domain?=?'www.baidu.com'
????expire_date?=?get_cert_expire_date(domain)
????print(expire_date)

4、通過socket 獲取域名ssl 證書信息

核心代碼

#?-*-?coding:?utf-8?-*-
import?socket
import?ssl
def?get_domain_cert(domain):
????"""
????獲取證書信息
????:param?domain:?str
????:return:?dict
????"""
????socket.setdefaulttimeout(5)
????cxt?=?ssl.create_default_context()
????skt?=?cxt.wrap_socket(socket.socket(),?server_hostname=domain)
????skt.connect((domain,?443))
????cert?=?skt.getpeercert()
????skt.close()
????return?cert
if?__name__?==?"__main__":
????print(get_domain_cert("www.baidu.com"))

還有一種方式也記錄一下

import?socket
import?ssl
def?get_domain_cert(host,?port=443,?timeout=3):
????"""
????獲取證書信息
????存在問題:沒有指定主機(jī)ip,不一定能獲取到正確的證書信息
????:param?host:?str
????:param?port:?int
????:param?timeout:?int
????:return:?dict
????"""
????context?=?ssl.create_default_context()
????with?socket.create_connection(address=(host,?port),?timeout=timeout)?as?sock:
????????with?context.wrap_socket(sock,?server_hostname=host)?as?wrap_socket:
????????????return?wrap_socket.getpeercert()

輸出

{
'subject': ((('countryName', 'CN'),), (('stateOrProvinceName', 'beijing'),), (('localityName', 'beijing'),), (('organizationalUnitName', 'service operation department'),), (('organizationName', 'Beijing Baidu Netcom Science Technology Co., Ltd'),), (('commonName', 'baidu.com'),)), 
'issuer': ((('countryName', 'BE'),), (('organizationName', 'GlobalSign nv-sa'),), (('commonName', 'GlobalSign RSA OV SSL CA 2018'),)), 
'version': 3, 
'serialNumber': '4417CE86EF82EC6921CC6F68', 
'notBefore': 'Jul  5 05:16:02 2022 GMT', 
'notAfter': 'Aug  6 05:16:01 2023 GMT', 
'subjectAltName': (('DNS', 'baidu.com'), ), 
'OCSP': ('http://ocsp.globalsign.com/gsrsaovsslca2018',), 
'caIssuers': ('http://secure.globalsign.com/cacert/gsrsaovsslca2018.crt',), 
'crlDistributionPoints': ('http://crl.globalsign.com/gsrsaovsslca2018.crl',)
}

結(jié)構(gòu)化輸出內(nèi)容后的完整代碼

#?-*-?coding:?utf-8?-*-
import?socket
import?ssl
from?dateutil?import?parser
#?requests.packages.urllib3.disable_warnings()
try:
????_create_unverified_https_context?=?ssl._create_unverified_context
except?AttributeError:
????#?Legacy?Python?that?doesn't?verify?HTTPS?certificates?by?default
????pass
else:
????#?Handle?target?environment?that?doesn't?support?HTTPS?verification
????ssl._create_default_https_context?=?_create_unverified_https_context
DATETIME_FORMAT?=?'%Y-%m-%d?%H:%M:%S'
socket.setdefaulttimeout(5)
def?get_domain_ip(domain):
????"""
????獲取ip地址
????:param?domain:?str
????:return:?str
????"""
????try:
????????addrinfo?=?socket.getaddrinfo(domain,?None)
????????return?addrinfo[0][-1][0]
????except?Exception?as?e:
????????pass
????return?None
def?get_domain_cert(domain):
????"""
????獲取證書信息
????:param?domain:?str
????:return:?dict
????"""
????cxt?=?ssl.create_default_context()
????skt?=?cxt.wrap_socket(socket.socket(),?server_hostname=domain)
????skt.connect((domain,?443))
????cert?=?skt.getpeercert()
????skt.close()
????return?cert
def?get_cert_info(domain):
????"""
????獲取證書信息
????:param?domain:?str
????:return:?dict
????"""
????cert?=?get_domain_cert(domain)
????issuer?=?_tuple_to_dict(cert['issuer'])
????subject?=?_tuple_to_dict(cert['subject'])
????return?{
????????'domain':?domain,
????????'ip':?get_domain_ip(domain),
????????'subject':?_name_convert(subject),
????????'issuer':?_name_convert(issuer),
????????#?'version':?cert['version'],
????????#?'serial_number':?cert['serialNumber'],
????????'start_date':?_parse_time(cert['notBefore']),
????????'expire_date':?_parse_time(cert['notAfter']),
????}
def?_tuple_to_dict(cert_tuple):
????"""
????cert證書?tuple轉(zhuǎn)dict
????:param?cert_tuple:?tuple
????:return:
????"""
????data?=?{}
????for?item?in?cert_tuple:
????????data[item[0][0]]?=?item[0][1]
????return?data
def?_name_convert(data):
????"""
????名字轉(zhuǎn)換
????:param?data:?dict
????:return:?dict
????"""
????name_map?=?{
????????'C':?'countryName',
????????'CN':?'commonName',
????????'O':?'organizationName',
????????'OU':?'organizationalUnitName',
????????'L':?'localityName',
????????'ST':?'stateOrProvinceName'
????}
????dct?=?{}
????for?key,?value?in?name_map.items():
????????dct[key]?=?data.get(value,?'')
????return?dct
def?_parse_time(time_str):
????"""
????解析并格式化時(shí)間
????:param?time_str:?str
????:return:?str
????"""
????return?parser.parse(time_str).astimezone().strftime(DATETIME_FORMAT)
if?__name__?==?"__main__":
????print(get_cert_info("www.baidu.com"))

輸出

{
  "domain": "www.baidu.com",
  "ip": "39.156.66.14",
  "subject": {
    "C": "CN",
    "CN": "baidu.com",
    "O": "Beijing Baidu Netcom Science Technology Co., Ltd",
    "OU": "service operation department",
    "L": "beijing",
    "ST": "beijing"
  },
  "issuer": {
    "C": "BE",
    "CN": "GlobalSign RSA OV SSL CA 2018",
    "O": "GlobalSign nv-sa",
    "OU": "",
    "L": "",
    "ST": ""
  },
  "start_date": "2022-07-05 13:16:02",
  "expire_date": "2023-08-06 13:16:01"
}

5、通過pyOpenSSL獲取證書信息

該方式,不校驗(yàn)證書合法性,只獲取證書信息

文檔:https://pyopenssl.org/en/0.15.1/index.html#

依賴

pip?install?pyOpenSSL

示例

#?-*-?coding:?utf-8?-*-
import?ssl
import?OpenSSL
def?get_ssl_expire_date(host,?port=443):
????cert?=?ssl.get_server_certificate((host,?port))
????x509?=?OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,?cert)
????return?x509.get_notAfter().decode()
if?__name__?==?'__main__':
????print(get_ssl_expire_date('www.baidu.com'))
????#?20230806051601Z

6、Domain Admin可視化管理域名證書到期

項(xiàng)目地址:https://github.com/mouday/domain-admin

運(yùn)行環(huán)境:Python 3.7.0

$?pip?install?domain_admin
#?升級到最新版本,可選
$?pip3?install?-U?domain-admin?-i?https://pypi.org/simple
#?啟動(dòng)運(yùn)行
$?gunicorn?'domain_admin.main:app'

訪問地址:http://127.0.0.1:8000

默認(rèn)的管理員賬號:admin 密碼:123456

以上就是python獲取域名ssl證書信息和到期時(shí)間的詳細(xì)內(nèi)容,更多關(guān)于python ssl的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python項(xiàng)目Docker倉庫發(fā)布指南

    Python項(xiàng)目Docker倉庫發(fā)布指南

    本文檔詳細(xì)介紹如何將 AI Backend Python 項(xiàng)目構(gòu)建為 Docker 鏡像并發(fā)布到各種 Docker 倉庫,包括 Docker Hub、阿里云容器鏡像服務(wù)、騰訊云容器鏡像服務(wù)等,需要的朋友可以參考下
    2025-08-08
  • 介紹一款python類型檢查工具pyright(推薦)

    介紹一款python類型檢查工具pyright(推薦)

    這篇文章主要介紹了介紹一款python類型檢查工具pyright(推薦),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 深入理解NumPy 的 np.column_stack的實(shí)現(xiàn)

    深入理解NumPy 的 np.column_stack的實(shí)現(xiàn)

    本文主要介紹了NumPy 的 np.column_stack的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-01-01
  • Pytorch?linear?多維輸入的參數(shù)問題

    Pytorch?linear?多維輸入的參數(shù)問題

    這篇文章主要介紹了Pytorch?linear多維輸入的參數(shù)的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • Python?設(shè)計(jì)模式創(chuàng)建型單例模式

    Python?設(shè)計(jì)模式創(chuàng)建型單例模式

    這篇文章主要介紹了Python?設(shè)計(jì)模式創(chuàng)建型單例模式,即Singleton,單例是一種設(shè)計(jì)模式,應(yīng)用該模式的類只會(huì)生成一個(gè)實(shí)例,下文詳細(xì)介紹需要的小伙伴可以參考一下
    2022-02-02
  • 解決Pycharm后臺indexing導(dǎo)致不能run的問題

    解決Pycharm后臺indexing導(dǎo)致不能run的問題

    今天小編就為大家分享一篇解決Pycharm后臺indexing導(dǎo)致不能run的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python使用numpy生成18種特殊數(shù)組

    python使用numpy生成18種特殊數(shù)組

    這篇文章主要介紹了python使用numpy生成18種特殊數(shù)組的方法,文章通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Python實(shí)現(xiàn)注冊登錄系統(tǒng)

    Python實(shí)現(xiàn)注冊登錄系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了適合初學(xué)者學(xué)習(xí)的Python3銀行賬戶登錄系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • python之MSE、MAE、RMSE的使用

    python之MSE、MAE、RMSE的使用

    今天小編就為大家分享一篇python之MSE、MAE、RMSE的使用,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python中處理時(shí)間的幾種方法小結(jié)

    Python中處理時(shí)間的幾種方法小結(jié)

    這篇文章主要介紹了Python中處理時(shí)間的幾種方法,包括時(shí)間的獲取和時(shí)間之間的轉(zhuǎn)換等等,需要的朋友可以參考下
    2015-04-04

最新評論

岑溪市| 怀宁县| 丹江口市| 平阳县| 成都市| 土默特左旗| 定远县| 宁远县| 辰溪县| 灵川县| 吉木乃县| 贵港市| 南漳县| 西昌市| 新蔡县| 鹿泉市| 霍州市| 普陀区| 页游| 平原县| 舒城县| 宜黄县| 密山市| 榆中县| 云南省| 个旧市| 鄂托克旗| 萨嘎县| 平罗县| 巴青县| 古蔺县| 云霄县| 凤翔县| 临安市| 斗六市| 日土县| 万安县| 芒康县| 微博| 东丽区| 阜阳市|