Email Ping挂在emai上

2024-09-28 19:25:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用一个脚本,我发现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循环已经解决了这个问题,但是现在它将挂在一封电子邮件上,而不是遍历列表的其余部分。这是一个项目,所以任何帮助,使其移动将不胜感激。你知道吗


Tags: csvnamenewfordataserveraddressemail
1条回答
网友
1楼 · 发布于 2024-09-28 19:25:06

如果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表示您将永远不会超时,但您的情况似乎不是这样。你知道吗

相关问题 更多 >