这篇博客整理一个脚本,用来在服务器上给自己发邮件,主要结合程序执行监测,在程序执行完毕之后给自己发邮件提醒,可以让自己及时查看结果并进行下一步的计算过着执行别的任务,做这个事情最主要的原因是想节省时间,同时也有自己懒的原因 {:.info}
发送文字 在前面监测程序运行的Shell脚本 这篇博客中,虽然提供了监测程序运行的脚本,但是任务是否执行完毕还是需要自己登录服务器进行查看,这里就想整理一个脚本,在程序执行完毕之后通过邮件方式进行提醒,这样可以让自己及时的查看计算结果并开始下一步的计划,极大的方便了自己,同时也可以节省不少的时间,专心去做别的事情.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import smtplibfrom email.mime.text import MIMETextimport sys,os mail_host = 'smtp.qq.com' port = 465 send_by = '********@qq.com' password = 'asdfghjkl' send_to = 'abcd@mail.com' def send_email (title,content, ): message = MIMEText(content,'plain' ,'utf-8' ) message["From" ] = send_by message['To' ] = send_to message['Subject' ] = title try : smpt = smtplib.SMTP_SSL(mail_host, port, 'utf-8' ) smpt.login(send_by,password) smpt.sendmail(send_by, send_to,message.as_string()) print ("发送成功" ) except : print ("发送失败" ) def main (): title = sys.argv[1 ] content = sys.argv[2 ] send_email(title,content) if __name__ == "__main__" : main()
这里只需要配置好自己要发送邮件的qq邮箱与接受邮件消息的邮箱即可,qq邮箱需要去开启STMP服务,这个可以自行百度解决.配置号邮箱之后,就只需要确定输入参数邮件标题title
与邮件内容content
.这里使用了sys.argv
这个函数,在使用这个python脚本的时候需要输入参数1 python3 filename.py "title" "content"
通过这样的执行之后,邮件标题即为title ,邮件内容为content .
实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # !/bin/sh # ============ get the file name =========== Folder_A=$(pwd) for file_a in ${Folder_A}/*.f90 do if [ -n "$file_a" ];then temp_file=`basename $file_a .f90` ifort -mkl -O3 -CB $file_a -o $temp_file.out if [ -e $temp_file.out ];then ./$temp_file.out & fi fi done python3 pathToMail/mail.py "Code Complier" "Your Code have been finished"
通过这个shell脚本,当文件夹中所有的fortran程序都编译执行完毕之后,就会发送一封邮件到我的邮箱提醒我任务进度.同样也可将这个发送邮件的python脚本和我前面监测程序运行的shell脚本结合,提醒程序执行进度,nice!!!!!
发送文字和图片 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 import smtplibimport os,sysfrom email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Headerfrom email import encodersfrom email.mime.base import MIMEBasefrom email.utils import parseaddr, formataddrimport base64from email.mime.image import MIMEImage import traceback class SendMail (object ): def __init__ (self, title=None , content=None , file=None , image=None , ssl_port=465 ): ''' :param username: 用户名 :param passwd: 密码(QQ邮箱SMTP开启后的一串字符) :param recv: 收件人,可以同时给多个人发送,此时需要使用list ['user1@qq.com','user2@qq.com] :param title: 邮件标题 :param content: 邮件正文 :param image: 图片路径,绝对路径,默认为无图片 :param file: 附件路径,如果不在当前目录下,要写绝对路径,默认没有附件 :param ssl: 是否安全链接,默认为安全链接 :param email_host: smtp服务器地址,默认为qq服务器 :param ssl_port: 安全链接端口,默认为465 ''' self .username = "123456789@qq.com" self .passwd = "abcdefghijklmn" self .recv = ['123456789@qq.com' ] self .title = title self .content = content self .file = file self .image = image self .email_host = 'smtp.qq.com' self .ssl = True , self .ssl_port = ssl_port self .message = MIMEMultipart() if self .file: file_name = os.path.split(self .file)[-1 ] try : f = open (self .file, 'rb' ).read() except Exception as e: traceback.print_exc() else : att = MIMEText(f, "base64" , "utf-8" ) att["Content-Type" ] = 'application/octet-stream' new_file_name = '=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?=' att["Content-Disposition" ] = 'attachment; filename="%s"' % (new_file_name) self .message.attach(att) if self .image: image_name = os.path.split(self .image)[-1 ] try : with open (self .image, 'rb' ) as f: mime = MIMEBase('image' , 'image' , filename=image_name) mime.add_header('Content-Disposition' , 'attachment' , filename=image_name) mime.set_payload(f.read()) encoders.encode_base64(mime) self .message.attach(mime) with open (self .image,'rb' ) as f: msgimage = MIMEImage(f.read()) msgimage.add_header('Content-ID' , '<image1>' ) self .message.attach(msgimage) except Exception as e: traceback.print_exc() def send_mail (self ): self .message.attach(MIMEText(self .content, 'html' , 'utf-8' )) self .message['From' ] = self .username self .message['To' ] = ',' .join(self .recv) self .message['Subject' ] = self .title self .smtp = smtplib.SMTP_SSL(self .email_host, port=self .ssl_port) self .smtp.login(self .username, self .passwd) try : self .smtp.sendmail(self .username, self .recv, self .message.as_string()) pass except Exception as e: resultCode = 0 traceback.print_exc() else : result = "邮件发送成功!" print (result) resultCode = 1 self .smtp.quit() return resultCode if __name__ == "__main__" : content = ''' <h1>''' + sys.argv[1 ] + '''</h1> <p>图片展示:</p> <p><img src="cid:image1"></p> ''' file = os.getcwd() + "/" + "loop.sh" image = os.getcwd() + "/" + "sushe2.png" m = SendMail("UNTITLE" ,content=content,file = file,image=image) m.send_mail()
执行命令1 python3 filename.py "test"
在Python
中可以利用模块os
实现和Linux下很多相同功能的操作,os.getcwd()
就可以获取当前文件的路径.
最后的结果如下图所示,可以将图片修改成自己计算的结果,这样就可以直接通过邮件查看结果,不用再登录服务器去查看结果.
公众号 相关内容均会在公众号进行同步,若对该Blog感兴趣,欢迎关注微信公众号。 {:.info}