发送电子邮件模块(smtplib)
大概思路:实例化smtp对象,连接smtp服务器以及开放的端口,调用starttls()方法建立安全链接,登录账户和授权码,发送邮件,最后退出
脚本示例:
#!/usr/bin/python
import smtplib
import string
host="smtp.qq.com"
subject="test email from python"
to="[email protected]"
send="[email protected]"
text='python rules them all!'
body = "\r\n".join((
"from: %s" % send,
"to: %s" % to,
"subject: %s" % subject,
"",
text
))
server=smtplib.smtp()
server.connect(host,"587")
server.starttls()
server.login("[email protected]","lgjahba")
server.sendmail(send,to,body)
server.quit()
探测web服务质量(pycurl)
思路:导入pycurl模块,实例化curl对象,设置测试属性,获取测试结果
import pycurl
import time
import sys
import os,sys
url=input("enter the url you want to query:\n ")
c=pycurl.curl()
c.setopt(pycurl.url,url)
c.setopt(pycurl.connecttimeout,5) #定义请求连接数
c.setopt(pycurl.noprogress,1) #屏蔽下载进度条
c.setopt(pycurl.forbid_reuse,1)#完成交互后强制断开连接,不重用
c.setopt(pycurl.maxredirs,1) #指定http重定向的最大数为1
c.setopt(pycurl.dns_cache_timeout,30) #设置保存dns信息的时间为30秒
indexfile=open(os.path.dirname(os.path.realpath(__file__))+"/content.txt","wb")
c.setopt(pycurl.writeheader,indexfile)
c.setopt(pycurl.writedata,indexfile)
try:
c.perform() #提交请求
except exception as e:
print("connection error:",str(e))
indexfile.close()
c.close()
sys.exit()
dns_time=c.getinfo(c.namelookup_time) #获取dns计息时间
connect_time=c.getinfo(c.connect_time) #获取建立连接的时间
pretransfer_time=c.getinfo(c.pretransfer_time) #获取从建立连接到准备传输的时间
starttransfer_time=c.getinfo(c.starttransfer_time) #获取从建立连接到传输开始的时间
total_time=c.getinfo(c.total_time) #获取传输的总时间
http_code=c.getinfo(c.http_code) #获取http状态码
size_downland=c.getinfo(c.size_download) #获取下载包大小
head_size=c.getinfo(c.header_size) #获取http头部大小
speed_downland=c.getinfo(c.speed_download) # 获取平均下载速度
print("http状态码:%s" % (http_code) )
print("dns解析时间:%.2f" % (dns_time))
print("建立连接时间:%.2f" % (connect_time))
print("准备传输时间:%.2f:" % (pretransfer_time))
print("传输开始时间:%.2f:" % (starttransfer_time))
print("传输结束总时间:%.2f" % (total_time))
print("下载数据包大小:%d byte" % size_downland)
print("http头部大小:%d byte" % head_size)
print("平均下载速度:%d bytes/s" % speed_downland)
indexfile.close()
c.close()