设为首页 加入收藏

TOP

Python自动化--语言基础7--操作日志、加密、发送邮件、线程、生产者消费者(二)
2017-12-23 06:07:16 】 浏览:623
Tags:Python 自动化 语言基础 7-- 操作 日志 加密 发送 邮件 线程 生产者 消费者
(open('send_mail_excel.xlsx', 'rb').read()) 23 # xlsxpart.add_header('Content-Disposition', 'attachment', filename='send_mail_excel.xlsx') 24 # msg.attach(xlsxpart) 25 26 #jpg图片附件 27 jpgpart = MIMEApplication(open('Aaron.png', 'rb').read()) 28 jpgpart.add_header('Content-Disposition', 'attachment', filename='Aaron.png') #需要图片文件在代码相应的目录下 29 msg.attach(jpgpart) 30 31 #发送邮件 32 smtp=smtplib 33 smtp=smtplib.SMTP() 34 smtp.set_debuglevel(1) #设置为调试模式,console中显示 35 smtp.connect('smtp.163.com','25') #链接服务器,smtp地址+端口 36 smtp.login('hanxxxxxx@163.com','Hanxxxxxxxx') #登录,用户名+密码 37 smtp.sendmail('hanxxxxxxx@163.com','123456789@qq.com',str(msg)) #发送,from+to+内容 38 smtp.quit() 39 40 mail = SendMail() 41 mail.send_mail('python自动化测试')
 
 

 查找最进时间修改的文件,代码如下:

 
 
 1 os.path.listdir  #以列表的形式展示文件
 2 os.path.getmtime #最后修改的时间
 3 os.path.join     #路径拼接
 4 
 5 import os
 6 filenames = "D:\\pycharm workspace\\appiumframework\\report"
 7 lists = os.listdir(filenames) 8 print(lists) 9 lists.sort(key=lambda fn:os.path.getmtime(filenames+"\\"+fn)) 10 print(lists[-1]) 11 file = os.path.join(filenames,lists[-1]) 12 print(file)

4、进程与线程的区别:

进程不共享空间,线程共享地址空间

线程共享空间优缺点:
优点:多线程给用户的体验好些,处理速度快些
缺点:共享地址空间相互影响

 1 import threading
 2 import time
 3 
 4 class Mythreading(threading.Thread):
 5     def __init__(self,threadID,name,counter):
 6         threading.Thread.__init__(self)    #固定格式
 7         self.threadID = threadID
 8         self.name = name
 9         self.counter = counter
10         print("初始化完成")
11     def run(self):                         #由cpu来处理决定线程间的执行顺序
12         print("开始"+self.name)
13         print_time(self.name,self.counter,5)
14         print("结束"+self.name)
15 
16 def print_time(threasName,counter,delay):
17     while counter:
18         time.sleep(delay)
19         print("%s:%s"%(threasName,time.ctime(time.time())))
20         counter -= 1
21 
22 #创建线程
23 thread1 = Mythreading(1,"thread1",1)
24 thread2 = Mythreading(2,"thread2",2)
25 
26 #开启线程
27 thread1.start()
28 thread2.start()
 1 import threading
 2 import time
 3 
 4 class Mythreading(threading.Thread):
 5     def __init__(self,threadID,name,counter):
 6         threading.Thread.__init__(self)    #固定格式
 7         self.threadID = threadID
 8         self.name = name
 9         self.counter = counter
10         print("初始化完成")
11     def run(self):                         #由cpu来处理决定线程间的执行顺序
12         threadLock.acquire()               #获得锁,成功获得锁定后返回True,可选的参数timeout不填时将一直阻塞直到获得锁定
13         print_time(self.name,self.counter,3)
14         threadLock.release()               #释放锁,开始下一个线程
15 
16 def print_time(threasName,counter,delay):
17     while counter:
18         time.sleep(delay)
19         print("%s:%s"%(threasName,time.ctime(time.time())))
20         counter -= 1
21 
22 threadLock = threading.Lock()
23 threads = []
24 
25 #创建线程
26 thread1 = Mythreading(1,"thread1",1)
27 thread2 = Mythreading(2,"thread2",2)
28 
29 #开启线程
30 thread1.start()
31 thread2.start()
32 
33 # thread1.join()
34 # thread2.join()
35 threads.append(thread1)
36 threads.append(thread2)
37 for t in threads:
38     t.join()       #后边的代码必须等待,等线程运行完成才会往后运行代码
39 
40 print("我的的花儿也谢了")

为什么下图左为串行,下图右为并行运行呢?

图左love启动后分别执行start和join,启动了join后边代码就需要等待前边代码运行完成。总共18s

图右同时启动love和hate,运行所需要执行的时间然后停止。总共10s

 超级播放器示例,如下:

 1 import threading
 2 from time import sleep, ctime
 3 def music(func):
 4     for i in range(2):
 5         print ("I was listening to %s! %s&
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python 内置函数 compile() 下一篇012函数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目