远程执行python脚本&邮件发送案例
场景:
#在linux中执行windoes的python脚本
一、安装wimrm模块(百度有资料)
#------------远程调用的代码-----------------
#!/usr/bin/env python # -*- coding: utf-8 -*- import winrm ''' Python中的pywirm模块可以很好的解决远程调用的问题 ''' wintest=winrm.Session('192.168.10.32:5985/wsman',auth=('Administrator','123456')) wintest.run_cmd("python D:/dalan/python/xuexi/test3/661.py")
二、邮件发送
1.发送包含标题&正文&附件
#!/usr/bin/env python # encoding: utf-8 from email.mime.multipart import MIMEMultipart from email.header import Header from email.mime.text import MIMEText import smtplib def send_email(subject, mail_body, file_names=list()): # 获取邮件相关信息 smtp_server = 'smtp.qq.com' port =465 user_name = '646572031@qq.com' password = '121211212121'#授权码,不是密码 sender = '646572031@qq.com' receiver = '1174873243@qq.com,18612260669@163.com' # 定义邮件内容 msg = MIMEMultipart() body = MIMEText(mail_body, _subtype="html", _charset="utf-8") msg["Subject"] = Header(subject, "utf-8") msg["From"] = user_name msg["To"] = receiver msg.attach(body) # 附件:附件名称用英文 for file_name in file_names: att = MIMEText(open(file_name, "rb").read(), "base64", "utf-8") att["Content-Type"] = "application/octet-stream" att["Content-Disposition"] = "attachment;filename='%s'" % (file_name) msg.attach(att) # 登录并发送邮件 try: smtp = smtplib.SMTP() smtp.connect(smtp_server) smtp.login(user_name, password) smtp.sendmail(sender, receiver.split(','), msg.as_string()) except Exception as e: print(e) print("邮件发送失败!") else: print("邮件发送成功!") finally: smtp.quit() if __name__ == '__main__': subject = "测试标题" mail_body = "测试本文123123" file_names = [r'D:\dalan\API_Automation\result\report.html'] send_email(subject, mail_body, file_names)
2.发送邮件正文使用html格式:
备注:html文件为report.html:

<table border = "1"> <h3>测试汇总:</h3> <tr> <th>执行时间</th> <th>通过率</th> <th>所有用例</th> <th>成功</th> <th>失败</th> </tr> <tr> <td>2020-10-22 00:01:12</td> <td>100%</td> <td>6</td> <td>4</td> <td>2</td> </tr> <tr> </table>
#!/usr/bin/env python # encoding: utf-8 import smtplib from email.mime.text import MIMEText sender = '646572031@qq.com' receiver = '1174873243@qq.com' subject = '这是测试邮件' smtpserver = 'smtp.qq.com' username = '646572031@qq.com' password = 'ufxuoifkuvutbcij' #通过变量定义html内容 html_text=''' <table border = "1"> <h3>测试汇总:</h3> <tr> <th>执行时间</th> <th>通过率</th> <th>所有用例</th> <th>成功</th> <th>失败</th> </tr> <tr> <td>2020-10-22 00:01:12</td> <td>100%</td> <td>6</td> <td>4</td> <td>2</td> </tr> <tr> </table> ''' ##通过文件获取html内容 with open(r'D:\dalan\API_Automation\result\report.html','r',encoding='UTF-8') as f: html_text=f.read() msg = MIMEText(html_text,'html','utf-8') msg['Subject'] = subject smtp = smtplib.SMTP() smtp.connect('smtp.qq.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) print('邮件发送成功') smtp.quit()
3.发送邮件正文使用html格式+附件:
#!/usr/bin/env python3 #coding: utf-8 import smtplib,time from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText def emali_enter(): sender = '646572031@qq.com'#发件人 receiver = ['1174873243@qq.com','chenwei@aidalan.com']#收件人 subject = 'Dalan-sdk接口测试失败汇总'#邮件主题 smtpserver = 'smtp.qq.com'#邮件服务器 username = '646572031@qq.com'#用户名 password = 'ufxuoifkuvutbcij' #密码 #主题实例化 msgRoot = MIMEMultipart('related') msgRoot['Subject'] = subject+time.strftime('%Y-%m-%d %H:%M:%S') #构造正文的html html = """ <!DOCTYPE html> <html> <body> <fieldset> <legend>注册界面</legend> <form> <label>账号<input type="text"></label><br><br> <label>密码<input type="password"></label><br><br> 性别<input type="radio" name="gender">男 <input type="radio" name="gender">女 <input type="radio" checked=“checked” name="gender">保密 <br><br> 爱好<input type="checkbox" name="aihao" checked=“checked”>足球 <input type="checkbox" name="aihao">篮球 <input type="checkbox" name="aihao">羽毛球 <input type="checkbox" name="aihao">唱歌 <input type="checkbox" name="aihao">跳舞 <br> 简介:<textarea cols="25" rows="5"></textarea><br><br> 生日<input type="date"><br><br> 邮箱<input type="email"><br><br> 电话<input type="number"><br><br> <input type="button" value="注册"> <input type="button" value="清空"> </form> </fieldset> </body> </html> """ text_html = MIMEText(html,'html', 'utf-8') #html = open(r'D:\dalan\python\xuexi\dalan\5555.html', 'rb').read() #直接通过文件读取html内容 msgRoot.attach(text_html) #构造附件 att = MIMEText(open(r'D:\dalan\API_Automation\result\最新报告.html', 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="test.html"' msgRoot.attach(att) #提交并发送邮件 smtp = smtplib.SMTP() smtp.connect('smtp.qq.com') smtp.login(username, password)#登录邮箱 smtp.sendmail(sender, receiver, msgRoot.as_string())#分别为发件人、收件人、邮件内容 print('邮件发送成功') smtp.quit()#退出邮件服务 if __name__ == '__main__': emali_enter()
相关资料:
https://www.cnblogs.com/linwenbin/p/10847910.html...................................................wimrm的配置(开启winrm service, 以便进行远程管理)
https://www.jb51.net/article/68187.htm.........................................................................wimrm启动失败解决办法(最好关闭防火墙)
http://t.zoukankan.com/gxgd-p-12980759.html...........................................................linux访问windows执行windows环境下的Python文件
https://www.cnblogs.com/angelyan/p/10566096.html ..................................................邮件发送
https://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html .................邮件发送汇总