Robot Framework通过Python SMTP进行email收发测试。
工作中需要对发送的邮件进行过滤,方法基本属于ACL控制,即查看“源/目的”邮件地址,邮件标题,邮件正文,邮件附件等进行过滤。
所以需要先模拟一下用Python能否达到邮件Client,Server的功能,还有能否在server上显示发送的 “源/目的”邮件地址,邮件标题,邮件正文,邮件附件 这些信息。
如果能取到这些信息,然后就可以对这些字符串进行查找,然后判断过滤功能是否work的目的了。
现在咱们先看下邮件服务器:
mailserver.py:
import smtpd
import asyncore class CustomSMTPServer(smtpd.SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data):
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
print 'data :', data
return server = CustomSMTPServer(('172.18.0.100', 9999), None) asyncore.loop()
这个就是邮件服务器,保存完后,直接双击运行就好,它会一直运行的。
UPDATE:
将此邮件服务器做成类似于linux服务:
1.将此程序放到/home目录下:
[root@Server init]# vim /home/mailserver.py import smtpd
import asyncore class CustomSMTPServer(smtpd.SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data):
self.peer = peer
self.mailfrom = mailfrom
self.rcpttos = rcpttos
self.data = data
mail_log = open('/home/current_mail.log','wb+')
mail_log.write(str('Receiving message from:')+ str(self.peer)+'\n')
mail_log.write(str('Message addressed from:')+ str(self.mailfrom)+'\n')
mail_log.write(str('Message addressed to :')+ str(self.rcpttos)+'\n')
mail_log.write(str('Message length :')+ str(len(self.data))+'\n')
mail_log.write(str('data :')+ str(self.data)+'\n')
mail_log.close()
return server = CustomSMTPServer(('172.16.0.3', 9999), None) asyncore.loop()
这个程序会在当前目录下创建邮件log,用来判断客户端发送的请求是否正确。
2.在/etc/init目录下(不是/etc/init.d)创建mailserver.conf这个文件,然后填写如下配置:
[root@Server init]# vim mailserver.conf
description "mailserver is in /home/mailserver.py"
author "xxx@xxx.com" start on runlevel []
stop on runlevel [!] #env AN_ENVIRONMENTAL_VARIABLE=i-want-to-set respawn exec /home/mailserver.py
3. 执行“initctl reload-configuration"命令
4. 然后可以通过以下命令启动或关闭邮件服务器:
start mailserver
stop mailserver
restart mailserver
邮件客户端:
Step1: 先在pyton目录下创建一个自定义的文件夹“SelfEmailLibrary”(您的Python安装到哪里就相应的放到哪里):
F:\Python27\Lib\site-packages\SelfEmailLibrary
Step2: 将下面的代码复制到“mailsend.py”和“__init__.py”中。
mailsend.py:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os class SendEmailUtility(object): ROBOT_LIBRARY_SCOPE = 'Global' def __init__(self):
print 'send email utility' def send_mail_with_attachment(self, from_user, from_password, to, subject, text, attach, mailserver, serverport):
self.mailserver = str(mailserver)
self.serverport = int(serverport)
msg = MIMEMultipart() msg['From'] = from_user
msg['To'] = to
msg['Subject'] = subject msg.attach(MIMEText(text)) part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
#Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part) mailServer = smtplib.SMTP(self.mailserver, self.serverport)
mailServer.ehlo()
#mailServer.starttls()
mailServer.ehlo()
#mailServer.login(from_user, from_password)
mailServer.sendmail(from_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close() def send_mail_no_attachment(self, from_user, from_password, to, subject, text,
mailserver, serverport):
self.mailserver = str(mailserver)
self.serverport = int(serverport) msg = MIMEMultipart() msg['From'] = from_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text)) mailServer = smtplib.SMTP(self.mailserver, self.serverport)
mailServer.ehlo()
#mailServer.starttls()
mailServer.ehlo()
#mailServer.login(from_user, from_password)
mailServer.sendmail(from_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
__init__.py:
from mailsend import SendEmailUtility
__version__ = '1.0' class SelfEmailLibrary(SendEmailUtility): ROBOT_LIBRARY_SCOPE = 'GLOBAL'
Step3:创建一个脚本测试一下这两个function是否工作:
mailclient.py:
from SelfEmailLibrary import * a = SendEmailUtility() a.send_mail_no_attachment(from_user="aaa@aaa.com", from_password='', to="bbb@bbb.com", subject="selftry",
text="asdlfkajsdlfd", mailserver="172.18.0.100", serverport="") a.send_mail_with_attachment(from_user="ddd@ddd.com", from_password='', to="ccc@ccc.com",
subject="selftry",text="bbbbbbbbbb", attach='simple.txt',
mailserver="172.18.0.100", serverport="")
Step4: 测试这俩函数对不对:
1. 先双击开启mailserver.py。
2. 执行mailclient.py。
3. 查看mailserver的显示是否正确:

可以看到,邮件的各个属性还有附件内容(simple.txt的内容就是“testkeyword”)都可以查看到。这样我们就可以通过查看mailserver收到的DATA来判断过滤功能了。
Note:
send_mail_with_attachment和send_mail_no_attachment两个函数中的from_password参数其实没有用,可以删掉。然后如果你想用base64加密,可以取消注销掉Encoders.encode_base64(part)这一行。 其实功能已经实现了,要是想让它在RobotFramework中work就是轻而易举的事儿了。
Robot Framework通过Python SMTP进行email收发测试。的更多相关文章
- robot framework用python扩展编写自定义library
我的utils.py文件 #!/usr/bin/env python #-*- coding:utf8 -*- __version__ = '0.1' import sys reload(sys) s ...
- robot framework自定义python库
自定义python库的好处: robot framework填表式,将python的灵活性弄没了,但是不要担心,RF早就想到了解决办法,就是扩充自己的库. 1.在python应用程序包目录下创建一个新 ...
- robot framework中如何为每个测试用例,测试集准备数据或销毁数据
Suite Setup:在这个测试集的所有测试用例开始测试之前运行(类似于junit的@BeforeClass) Suite Teardown:在这个测试集的所有测试用例结束之后运行(类似于junit ...
- 2小时入门Robot Framework
1.介绍 1.1.介绍Robot Robot Framework是一个基于关键字驱动的自动化测试框架.通过该框架,测试人员可使用python封装关键字,并在非代码环境下使用关键字构建可被执行的测试用例 ...
- Robot Framework自动化测试(四)--- 分层思想
谈到Robot Framework 分层的思想,就不得不提“关键字驱动”. 关键字驱动: 通过调用的关键字不同,从而引起测试结果的不同. 在上一节的selenium API 中所介绍的方法其实就是关 ...
- Robot Framework测试框架学习笔记
一.Robot Framework框架简介 Robot Framework是一种基于Python的可扩展关键字驱动自动化测试框架,通常用于端到端的可接收测试和可接收测试驱动的开发.可以 ...
- 【转】Robot Framework 快速入门
目录 介绍 概述 安装 运行demo 介绍样例应用程序 测试用例 第一个测试用例 高级别测试用例 数据驱动测试用例 关键词keywords 内置关键词 库关键词 用户定义关键词 变量 定义变量 使用变 ...
- Robot Framework 使用1-环境配置及简单网站兼容性测试(转)
0.Robot Framework 简介 Robot Framework 是一个通用的自动化测试框架,主要用于“验收测试”和“验收测试驱动开发(ATDD)” (会其它文章中会详细介绍ATDD).它使用 ...
- robot framework + win7 64 上的安装
1.安装 python 2.7 2.cmd 管理模式 python -m pip install --upgrade pip pip install robotframework==3. ...
随机推荐
- 多线程:InterlockedIncrement
1.InterlockedIncrement保护多线程中操作的整数. #include <stdio.h> #include <windows.h> volatile long ...
- 推荐一个WebIDE在线编程语言编译器C9.io
有时借用别人电脑或者不想在电脑上安装各种乱七八糟的IDE,就可以考虑 Web IDE.随着Web技术发展,很多语言的编译工作都可以利用Web 浏览器来完成. 1. 推荐国外的 C9.io 个人可以免费 ...
- 问题009:java当中的关键字有哪些?在Editplus文本编辑软件中是什么颜色的?java当中的标识符有什么要求?Java中注释分为几类?
(1) public 公共的,表示访问的权限 (2) private 私有的,表示一种访问权限 (3) class 类关键字,表示定义一个类 java中的关键字都是大写的还是小写的?小写的,在Edit ...
- hello spring boot neo4j
新建springboot 项目: https://www.cnblogs.com/lcplcpjava/p/7406253.html bug fixs: 1. Maven Configuration ...
- 【思维题 经典模型】cf632F. Magic Matrix
非常妙的经典模型转化啊…… You're given a matrix A of size n × n. Let's call the matrix with nonnegative elements ...
- Apache 错误日志
配置文件中:httpd.conf ErrorLog "/usr/local/var/log/apache2/error_log1" CustomLog "/usr/loc ...
- React学习记录一
半路出家直接上手React,其实有点吃力,所以开始研究create-react-app,从这里下手吧. create-react-app 官方网站:https://github.com/faceboo ...
- 22.VUE学习之-replice删除当前评论条数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 笔记1 python入门学习笔记
目录 官方手册 菜鸟站手册地址: python的运行方法 注释 小技巧: input()接收用户输入的内容(默认为字符串) print() 运算符 is 是判断两个标识符是不是引用自一个对象 all和 ...
- Python知识点进阶——生成器
生成器 为什么要将列表转化为迭代器? 因为列表太大的话用内存太大,做成迭代器可以节省空间,用的时候再拿出部分. 生成器是不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,知 ...