最开始使用pip install whois,提示Successfully installed whois-0.7,可是执行相关whois.whois("www.sohu.com")命令时,提示如下错误:
AttributeError: module 'whois' has no attribute 'whois'
然后再安装pip install python-whois,然后可以用了,不过只成功了一次,又不能用了。
最后将whois-0.7,python-whois全部卸载掉,再重新安装python-whois,终于可以用了。
附最终代码,可以放到树莓派上定时执行。
import whois
import datetime,time
import smtplib #加载smtplib模块
from email.mime.text import MIMEText
from email.utils import formataddr
def get_days(str1, str2):
date1 = datetime.datetime.strptime(str1[0:10], "%Y-%m-%d")
date2 = datetime.datetime.strptime(str2[0:10], "%Y-%m-%d")
num = (date1 - date2).days
return num
my_sender='xxx@163.com'
my_user='xxx@qq.com'
def mail():
ret=True
try:
msg=MIMEText('您的域名即将过期,请留意!','plain','utf-8')
msg['From']=formataddr(["网易",my_sender]) #括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To']=formataddr(["林风",my_user]) #括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject']="域名到期通知" #邮件的主题,也可以说是标题
server=smtplib.SMTP("smtp.163.com",25) #发件人邮箱中的SMTP服务器,端口是25
server.login(my_sender,"xxx") #括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(my_sender,[my_user,],msg.as_string()) #括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() #这句是关闭连接的意思
except Exception: #如果try中的语句没有执行,则会执行下面的ret=False
ret=False
return ret
if __name__ == '__main__':
today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
for line in open("domain.txt"):
print(line)
domain = whois.whois(line)
expiration_date = domain.expiration_date
notify_date = str(expiration_date - datetime.timedelta(days=1))
days = get_days(notify_date,today)
if days <= 3:
ret = mail()
if ret:
print("邮件发送成功!") # 如果发送成功则会返回ok,稍等20秒左右就可以收到邮件
else:
print("filed") # 如果发送失败则会返回filed
https://github.com/D4Vinci/Domain-Checker
https://github.com/jakehu/check-domain/blob/master/check.php
https://www.dropcatch.com
https://github.com/crock/dropcatch-filter