Operation and maintenance tips: Obtain the validity period of the domain name through Python

In IT operation and maintenance, many companies have a large amount of domain name information, and need to regularly check whether it is expired and renewed. whoisTo obtain the validity period of the domain name, you need to query the Whois record of the domain name, which can be queried using the Python library. The example code is as follows:

from whois import whois
import datetime

domain = 'baidu.com'

# 查询域名的 Whois 记录
w = whois(domain)

# 获取域名的到期日期
if isinstance(w.expiration_date, list):
    expiry_date_obj = w.expiration_date[0]
else:
    expiry_date_obj = w.expiration_date

# 计算到期日期和当前日期之间的天数差
days_left = (expiry_date_obj - datetime.datetime.now()).days

print(days_left)

In the above code, we first whoisqueried the Whois record for the domain name using the library and extracted the expiration date from it. We then calculated the difference in days between the expiration date and the current date to obtain the remaining validity period of the domain name.

Note: This method relies on a properly configured Whois server and Whois records, so it may not work for all domain names. Some domain names may hide their Whois information, while others may use a different format for Whois data than the standard one.

The above is the sharing content of this issue. Hello everyone, I am Lele, focusing on the research and sharing of operation and maintenance technology, follow me, and learn more about operation and maintenance knowledge. If you have any questions, you can also leave a message in the Lewei community to ask questions, and discuss with the majority of operation and maintenance technology enthusiasts.

Guess you like

Origin blog.csdn.net/weixin_43631631/article/details/130407421