Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 电子邮件Ping挂在电子邮件上_Python_While Loop_Try Catch_Email Validation - Fatal编程技术网

Python 电子邮件Ping挂在电子邮件上

Python 电子邮件Ping挂在电子邮件上,python,while-loop,try-catch,email-validation,Python,While Loop,Try Catch,Email Validation,我正在尝试使用我找到的脚本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-\\+]+(\\

我正在尝试使用我找到的脚本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循环已经解决了这个问题,但是现在它将挂起一封电子邮件,而不是遍历列表的其余部分。这是针对一个项目的,因此,如果您能帮助它移动,我们将不胜感激。

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

由于您关心的唯一错误是
TimeoutError
,因此您应该明确捕获该错误,并且
仅在这种情况下继续
。对于所有其他错误,要么打印错误并中断循环,要么让错误冒泡。创建服务器实例时,还应将超时设置为正常值。最后,最好使用
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
表示您将永远不会超时,但您的情况似乎并非如此