【问题标题】:Email Ping hangs on email电子邮件 Ping 挂在电子邮件上
【发布时间】:2017-06-03 10:03:10
【问题描述】:

我正在尝试使用我找到的脚本来 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('me@domain.com')
                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 循环已经解决了这个问题,但现在它会挂在一封电子邮件上,而不是遍历列表的其余部分。这是针对一个项目的,因此我们将不胜感激。

【问题讨论】:

    标签: python while-loop try-catch email-validation


    【解决方案1】:

    如果遇到任何类型的持续错误,您的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 的值表示您永远不会超时,但您似乎并非如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2015-05-20
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多