• 自动化测试第十节---多线程、继承、列表推导、发邮件


    1、列表推导:

    举例:生成100以内奇数的list

    ‘‘‘如果i是数,for循环处理后得到的值i会循环传入number列表‘‘‘
    names=['nyy','wxc',1234,['aaa'],'丹丹'
    number=[i for i in range(1,100) if i%2!=0]
    print(number)

    2、继承
    class AxFarther(object):
    def __init__(self,op):
    self.op=op
    print('父类:',op)
    def makeMoney(self):
    print("1000W")
    # axf=AxFarther('抽烟,喝酒')#打印“抽烟,喝酒”

    # #重写父类的构造方法
    class Ax(AxFarther):
    def __init__(self,op,code):
    print('子类:',code)
    self.code = code
    def makeMoney(self):#重写父类的方法
    print('子类方法:', "2000W")
    ax=Ax('抽烟,喝酒','python')#打印“子类: python”
    ax.makeMoney()#打印"子类方法: 2000W"

    #修改父类构造方法,在父类构造方法基础上,添加新功能
    class Ax(AxFarther):
    def __init__(self,op,code):
    AxFarther.__init__(self,op)#先把原来的调用一下,有父类的功能,如果想修改父类的构造方法,就先调用以下父类的构造方法,经典类必须这么写,新式类两种都行
    # super(Ax,self).__init__(op)#作用同上,super里传的是本类,super会自动找到父类的构造方法
    print('子类:',code)
    self.code=code
    def makeMoney(self):#重写父类的方法
    print('子类方法:',"2000W")
    ax=Ax('抽烟,喝酒','python')#打印父类: 抽烟,喝酒/n子类: python
    ax.makeMoney()#打印"子类方法: 2000W"

    3、多线程
    #程序一运行,首先有个主线程
    #循环创建10个子线程,让这十个子线程去运行axb这个函数
    #主线程继续走,打印了'game over'
    import threading,time#每个程序默认有一个主线程
    def axb(name):
    # time.sleep(1)
    print('hahaha',name)
    print(time.strftime('%Y%m%d%H%M%S',time.localtime()))#同时启动,时间一致
    for i in range(1000):#循环启动十个线程,执行函数axb,子线程,threading启动的都是子线程
    t = threading.Thread(target=axb, args=(i,)) # 实例化一个线程,启动一个线程
    t.start()
    print('game over')

    #怎么获取到多线程执行的函数里面的返回值
    import time, requests,threading
    run_times=[]
    objs=[]
    def blog(url):
    s_time=time.time()
    r=requests.get(url).text
    e_time=time.time()
    run_time=e_time-s_time
    run_times.append(run_time)
    url='http://www.nnzhp.cn/archives/527'
    for i in range(100):#通过多线程指定运行的函数,不能获取到函数的返回值
    t=threading.Thread(target=blog,args=(url,))
    t.start()
    objs.append(t)
    for obj in objs:#等待所有子线程执行完
    obj.join()
    avg=sum(run_times)/len(run_times)
    print("平均时间是:",avg)
    4、多进程
    from multiprocessing import Process
    import time
    def test(i):
    time.sleep(1)
    print(i)
    if __name__=='__main__':
    for i in range(10):
    p = Process(target=test, args=(i,))
    p.start()
    5、守护线程
    #主线程执行完了,那么不管子线程有没有执行完,都一起结束
    import time,threading
    def test():
    time.sleep(2)
    print('hahaha')
    for i in range(5):
    t=threading.Thread(target=test)
    t.setDaemon(True)#设置子线程为守护线程(此程序中主线程建立完子线程后就结束了,所以什么都打印不出来)
    t.start()
    6、锁
    # from threading import Lock
    a=0
    # lock=Lock()#申请一把锁
    def test():
    global a
    # lock.acquire()#加锁,python3可以不用手动加锁
    a+=1
    # lock.release()#解锁,python3可以不用手动解锁
    import threading
    #多线程在处理一个任务的时候,同时操作一个变量
    for i in range(1000):
    t = threading.Thread(target=test)
    t.start()
    print(a)

    7、例子:保存多个html-多线程-爬虫
    import requests,threading,time
    def write_html(url,name):
    r = requests.get(url)
    with open(name,'w',encoding='utf-8') as fw:
    fw.write(r.text)
    urls=['www.nnzhp.cn','besttest.cn','www.imdsx.cn','sb.nnzhp.cn','bbs.besttest.cn']
    objs = []#存放每个线程
    start_time = time.time()
    for url in urls:
    new_url = 'http://'+url
    file_name = url+'.html' #www.nnzhp.cn.html
    t = threading.Thread(target=write_html,args=(new_url,file_name))
    objs.append(t)
    t.start()
    # t.join()#主线程等待
    #.join就是主线程在等待每个子线程执行完成。
    #1、先启动10个线程,让他们在跑着
    #2、主线程再等他们
    # for obj in objs:
    # print('每次的obj',obj)
    # obj.join()
    # write_html(new_url,file_name)
    end_time = time.time()
    print('程序总共运行了',end_time-start_time)
    # cases
    # a.xls b.xls c.xls
    8、发邮件-带有附件
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    # from email.header import Header
    def send_mail(sender,pwd,receiver,content,subject,mailhost='smtp.qq.com',port=465):
    msg = MIMEMultipart()
    file = 'a.txt'
    att = MIMEText(open(file, encoding='utf8').read()) # 发送的附件对象
    att["Content-Type"] = 'application/octet-stream'
    att["Content-Disposition"] = 'attachment;filename="%s"' % file
    msg.attach(att) # 把附件添加到邮件里面
    msg.attach(MIMEText(content)) # 邮件正文内容添加到msg邮件对象里面
    msg['Subject']=subject
    msg['From']=sender
    msg['To']=receiver

    smtp=smtplib.SMTP_SSL(mailhost,port)
    smtp.login(sender,pwd)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()
    print('email send success.')

    sender = 'XXX' # 发送者账号
    pwd = 'XXX' # 发送者密码
    receiver = 'XXX'
    subject = '测试邮件标题'
    content = '这里是邮件内容'
    send_mail(sender,pwd,receiver,content,subject)

    9、发送普通邮件
    import smtplib
    from email.mime.text import MIMEText
    def send_mail(sender,pwd,receiver,content,subject,mailhost='smtp.qq.com',port=465):
    mail=MIMEText(content)
    mail['Subject']=subject
    mail['From']=sender
    mail['To']=receiver
    smtp=smtplib.SMTP_SSL(mailhost,port)
    smtp.login(sender,pwd)
    smtp.sendmail(sender,receiver,mail.as_string())
    smtp.quit()
    print('email send success.')

    sender = 'XXX' # 发送者账号
    pwd = 'XXX' # 发送者密码
    receiver = 'XXX'
    subject = '测试邮件标题'
    content = '这里是邮件内容'
    send_mail(sender,pwd,receiver,content,subject)
     


     
     
  • 相关阅读:
    Leetcode_02【两数相加】——【难度:中】
    Leetcode_39【组合总和】
    Leetcode_38【报数】
    Leetcode_36【有效的数独】
    Leetcode_35【搜索插入位置】
    51nod1347 旋转字符串
    WebH
    ExcelHelper
    文件二进制与String相互转换
    汇编语言里 eax, ebx, ecx, edx, esi, edi, ebp, esp
  • 原文地址:https://www.cnblogs.com/ninanie/p/8045947.html
Copyright © 2020-2023  润新知