需求:
我要查詢百度域名的到期時間或者開始時間
思路分析:
如果在linux系統中直接使用下面命令即可:
echo | openssl s_client -servername www.baidu.com -connect www.baidu.com:443 2>/dev/null | openssl x509 -noout -dates|egrep ‘notAfter'|awk -F'=|GMT' ‘{print $2}'
但是這個命令使用python2 的commands執行不成功,所以只能換成通過shell腳本去執行。
init_sh函數檢查shell腳本不存在則創建,這樣不需要多寫一個腳本,有程序生成。
#!/usr/bin/python
# -*- coding: utf-8 -*-
# author: chentufeng
# create time: 2020 12 25
import commands,os
script_sh = ".tmp.sh"
# 自動生成shell腳本用來執行shell命令獲取時間
def init_sh():
if not os.path.exists(script_sh):
with open(script_sh, 'w') as file_object:
file_object.write("yuming=$1\ntag=$2\n"
"ymtime=`echo | openssl s_client -servername $yuming -connect $yuming:443 2>/dev/null | openssl x509 -noout -dates|egrep \"$tag\"|awk -F'=|GMT' '{print $2}'`\n"
#時間轉換,如果需要也可以轉換成其他格式
"date -d \"$ymtime\" '+%Y-%m-%d %H:%M:%S'\n")
if __name__ == '__main__':
#初始化函數
init_sh()
yuming = "www.baidu.com"
tag = "notBefore" #notBefore 開始時間;notAfter 到期時間
cmd = "sh %s %s %s"%(script_sh, yuming, tag)
restatus,retime = commands.getstatusoutput(cmd)
print("獲取的時間:%s"%retime)
輸出結果:
到期時間
[root@測試機 ~]# python aa.py
獲取的時間:2021-07-26 05:31:02
開始時間
[root@測試機 ~]# python aa.py
獲取的時間:2020-04-02 07:04:58
到此這篇關于python 獲取域名到期時間的方法步驟的文章就介紹到這了,更多相關python 獲取域名到期時間內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python實現從url中提取域名的幾種方法
- Python的Flask框架中SERVER_NAME域名項的配置教程
- Python實現獲取域名所用服務器的真實IP
- Python的Flask框架中配置多個子域名的方法講解
- Python實現通過解析域名獲取ip地址的方法分析
- Python腳本實現DNSPod DNS動態解析域名
- Python基于whois模塊簡單識別網站域名及所有者的方法
- 利用Python+阿里云實現DDNS動態域名解析的方法
- python 域名分析工具實現代碼
- Python 實現域名解析為ip的方法
- Python批量查詢域名是否被注冊過