python实现检测服务器开启状态 并发送 QQ邮件发送
QQ邮箱授权码如何开启—-百度
# coding=utf-8
import os
import socket
import time
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from threading import Timer
def sendmail(qq_email,port):
#qq邮箱smtp服务器
host_server = 'smtp.qq.com'
#sender_qq为发件人的qq号码
sender_qq = 'XXXXXXXXXX'
#pwd为qq邮箱的授权码
pwd = 'XXXXXXXXXXX'
#发件人的邮箱
sender_qq_mail = 'XXXXXXX@qq.com'
#收件人邮箱
receiver = qq_email;
#邮件的正文内容
mail_content = '内容测试'
#邮件标题
mail_title = str(port)+' 这是标题 !!'
#ssl登录
smtp = SMTP_SSL(host_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(0)
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd)
msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
def IsOpen(ip,port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((ip,int(port)))
s.shutdown(2)
print '%d is open' % port
return False
except:
print '%d is down' % port
sendmail('XXXXXXXXX@qq.com',port);
if __name__ == '__main__':
while True:
IsOpen("127.0.0.1",8888)
IsOpen("127.0.0.1",8887)
time.sleep(9000)
#sendmail();