1. 如何实现python设备的告警
1、如果发现打不开了,直接发邮件提示网站已经打不开
2、发现可以打开,读取文件中上一次访问的情况(读取txt文件最后一行),
1)如果发现上一次是打不开的,发邮件提醒网站已经恢复了
2)如果发现上一次是打得开的(200的返回码),只是记录网站访问的日志就可以了
2. 如何用python写一个程序对windows服务器进行监控,如果出现异常则发送
用没有人访问网页来作为判断服务器是否挂的标准,这个不太靠谱吧。
建议是定时访问你的网页,当不能收到服务器的响应时(比如502,或者响应超时等等),判定为服务器挂掉,发送邮件给你。
3. 如何用python自动发送邮件
注意如果使用qq的smtp转发功能的话,需要在设置页面中将该功能打开,然后设定密码,改密码就是赋值给下文password中的。
开启smtp转发功能文档
[python] view plain copy
#from email.MIMEText import MIMEText #old version cannot recongnize by new python
from email.MIMEText import MIMEText
from email.Header import Header
msg = MIMEText('hello, send by python','plain','utf-8');
from_addr = "*****@qq.com" #email addresss include domain name
password = "******" #password
to_addr = "****@qq.com" # target email address
smtp_server = "smtp.qq.com" #smtp sever domain for qq is smtp.qq.com
import smtplib
server = smtplib.SMTP(smtp_server,25);
server.set_debuglevel(1)
server.login(from_addr,password);
server.sendmail(from_addr,[to_addr], msg.as_string())
server.quit()
上面这个是没有主题的,有主题的话建议采用下面这个代码
下面是连续发送有主题的文件10封
[python] view plain copy
from email.Header import Header
from email.MIMEText import MIMEText
from email import encoders
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
from_addr = "*****@qq.com" #email addresss include domain name
password = "******" #password
to_addr = "****@qq.com" # target email address
smtp_server = "smtp.qq.com" #smtp sever domain for qq is smtp.qq.com
for num in range(1,11):
msg = MIMEText('hello, send by Tom', 'plain', 'utf-8');
msg['From'] = _format_addr('Tom ' % from_addr)
msg['To'] = _format_addr('addministrator ' % to_addr)
msg['Subject'] = Header('The greeting from Tom','utf-8').encode()
server = smtplib.SMTP(smtp_server,25)
server.set_debuglevel(1)
server.login(from_addr, password)
#server.sendmail(from_addr, [to_addr], msg.as_string())
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
4. 利用python实现定时发送邮件功能,有大神么
一、文件形式的邮件 复制代码 代码如下: #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header sender = '***' receiver = '***' subject = 'python email test' smtps...
5. python脚本需要发送邮件报警需要安装什么服务
python本身自带发邮件模块
6. python 自动发邮件的方法怎么写
一、文件形式的邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
二、HTML形式的邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = MIMEText('
你好
','html','utf-8')
msg['Subject'] = subject
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
三、带图片的HTML邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgText = MIMEText('Some HTML text and an image.
good!','html','utf-8')
msgRoot.attach(msgText)
fp = open('h:\\python\\1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
四、带附件的邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
#构造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
五、群邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
sender = '***'
receiver = ['***','****',……]
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = MIMEText('你好','text','utf-8')
msg['Subject'] = subject
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
六、各种元素都包含的邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
Hi!
How are you?
Here is the link you wanted.
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
七、基于SSL的邮件
复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
7. 邮件提示功能是如何用实现的,python能做吗
用Python是可以做的,
8. 如何使用Python对邮件进行实时收取
我用poplib写了个收取邮件的脚本,但是总提示“poplib.error_proto: -ERR 登录太频繁!”,不知道是不是刷新太频繁了,可是怎么做到实时收取呢?比如那些邮件客户端
你研究 推送服务器,长链接 等等关键词,搜索一下,我没有完整做过,只是知道有这么个概念,所以只能帮你到这里。
服务器不允许就不要那么频繁了