2017-01-18 32 views
0

我试图使用我发现的脚本ping通电子邮件,以确保它们存在。电子邮件Ping挂在电子邮件

with open(input_list, 'r') as f: 
    reader = csv.reader(f) 
    for row in reader: 
     address = row[0] 
     person_name = row[1]+' '+row[2] 
     company = row[4] 
     match = re.match('^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$', address) 
     print("Email for ", person_name) 
     print(address) 
     if match == None: 
      synt = 'Bad Syntax' 
      warnings.warn(address + " has bad syntax.") 
     else: 
      synt = 'Good syntax' 
     dom = re.search("@(.*)$", address).group(1) 
     print(dom) 
     try: 
      records = dns.resolver.query(dom, 'MX') 
      mxRecord = records[0].exchange 
      mxRecord = str(mxRecord) 
     except: 
      warnings.warn("Issue contacting domain") 
      pass 
     # Get local server hostname 
     host = socket.gethostname() 
     # SMTP lib setup (use debug level for full output) 
     server = smtplib.SMTP('smtp-mail.outlook.com',587)#will need this for mail sending 
     while True: 
      try: 
       server.set_debuglevel(0) 
       # SMTP Conversation 
       server.connect(mxRecord) 
       server.helo(host) 
       server.mail('[email protected]') 
       code, message = server.rcpt(str(address)) 
       server.quit() 
       if code == 250: 
        print('Success') 
        new_row = [address, person_name, company, synt, 'Ping Successful'] 
        email_data.append(new_row) 
        with open('cseresult2.csv', 'a+', newline='') as mess: 
         writ = csv.writer(mess, dialect='excel') 
         writ.writerow(email_data[-1]) 
       else: 
        print('Bad') 
        new_row = [address, person_name, company, synt, 'Ping Bounced'] 
        email_data.append(new_row) 
        with open('cseresult2.csv', 'a+', newline='') as mess: 
         writ = csv.writer(mess, dialect='excel') 
         writ.writerow(email_data[-1]) 
      except: 
       continue 
      break 
     print() 
     print('================') 
     print() 
     time.sleep(3) 

该代码工作大多好。然而,跳出while循环,我得到了很多超时错误:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 

while循环已经慎重了,但现在它挂在电子邮件上通过列表的其余部分不重复。这是一个项目,所以任何帮助,让它移动,将不胜感激。

回答

0

如果您的while环路遇到任何类型的持续错误,它将被设置为永久继续。 try区块中有很多代码,因此有很多情况可能发生(例如,无法连接到您的电子邮件服务器,未能打开您的csv文件等)。没有更多的信息,我不能肯定地说这种情况正在发生,但是解决这个问题肯定不会有什么坏处。

由于您所关心的唯一错误是TimeoutError,因此您应该明确地捕获该错误,并且仅在该情况下才会收到该错误并且continue。对于所有其他错误,要么打印错误并打破循环,要么让错误冒泡。创建服务器实例时,您还应该将超时值设置为理智的值。最后,如果在一段时间后超时未解决,则最好使用for循环,该循环结束;即使在持续的TimeoutError的情况下,你不想尝试永远:

### Set a sane timeout 
server = smtplib.SMTP('smtp-mail.outlook.com', 587, timeout=1) 

### Try a maximum of 10 times 
for i in range(10): 
    try: 
     ### Connect to the email server and write to your file 

    except TimeoutError, e: 
     print e 

    ### All other errors will bubble up 

如果你是好奇默认的超时时间是什么,你可以这样做:

import socket 

print(socket.getdefaulttimeout()) 

的反应是默认超时,以秒为单位。值为None表示您永远不会超时,这似乎不适合您。