December 22, 2020
Вро де бы как сегда
сысысысы+мвымвы ывмы+
ывывывы
ясясясы
- ысысысы
- ысысы
- ысысы
- сммсмсмс
- смсмссм
- смссмс
счясясячсячс
ячя
import smtplib import dns.resolver from config import settingsfrom loguru import loggerfrom db import execute_tunnel, executefrom multiprocessing import Pool, current_processfrom sshtunnel import SSHTunnelForwarder# print(socket.getfqdn())def check_email(email: str, detailed_result: bool = False, logging = False): if logging: logger.info(f"Start checking {email}") if email.count('@') == 1: # if '@' in email: user, domain = email.split("@") try: mx_list = dns.resolver.query(domain, "MX") except dns.resolver.NoNameservers: if logging: logger.info(f"NoNameservers for {email}") if detailed_result: return False, 'NoNameservers' else: return False except dns.resolver.NoAnswer: if logging: logger.info(f"NoAnswer for {email}") if detailed_result: return False, 'NoAnswer' else: return False except dns.exception.DNSException: if logging: logger.info(f"DNSException for {email}") if detailed_result: return False, 'DNSException' else: return False except Exception as e: if logging: logger.info(f"{email}: {e}") if detailed_result: return False, 'Exception' else: return False if logging: logger.info(f'{domain}: ok') mx_list = [{"server": str(mx.exchange), "rate": mx.preference} for mx in mx_list] for mx in sorted(mx_list, key=lambda i: i["rate"]): try: while str(mx["server"]).endswith('.'): mx["server"] = mx["server"][0:-1] if logging: logger.info(f"Check mx: {mx['server']}") smtp = smtplib.SMTP(mx["server"], 25, timeout=2) smtp.helo() smtp.mail(settings.bot.smtp.sender) r = smtp.rcpt(email) if r[0] == 250: smtp.quit() if logging: logger.info(f"{email}: OK {r}") if detailed_result: return True, 'Code250' else: return True elif r[0] == 550: smtp.quit() if logging: logger.info(f"{email}: Bad {r}") if detailed_result: return False, 'Code550' else: return False except Exception as e: if logging: logger.error(f"{email}: {e}") if logging: logger.info(f"{email}: Unknown") if detailed_result: return True, 'Unknown' else: return True else: if detailed_result: return False, 'ErrorOf@' else: return Falsedef worker(row): process_name = current_process().name status = 'bad' check_result = check_email(row['email'], detailed_result=True) if check_result[0]: status = 'good' execute(f"UPDATE echeck SET status='{status}', status_detailed='{check_result[1]}' WHERE id={row['id']}") logger.info(f"{process_name}: {row['email']} : {status}")if __name__ == '__main__': # with SSHTunnelForwarder( # ("95.216.40.221", 36322), # ssh_username="web", # ssh_password="webIs27!3", # remote_bind_address=("127.0.01", 3306), # local_bind_address=("127.0.0.1", 3306), # # ) as TUNNEL: # rows = execute('SELECT * FROM echeck') # rows = execute("SELECT id, email FROM echeck WHERE status='na' or status_detailed is NULL or status_detailed ='Unknown' or status_detailed =''") rows = execute("SELECT id, email FROM echeck WHERE status=''") logger.info(f'Start checking {len(rows)} e-mails') with Pool(processes=4) as pool: results = pool.map(worker, rows) # for row in rows: # status = 'bad' # check_result = check_email(row['email'], detailed_result=True) # if check_result[0]: status = 'good' # execute_tunnel(f"UPDATE echeck SET status='{status}', status_detailed='{check_result[1]}' WHERE id={row['id']}") # print(check_email('[email protected]')) # print(check_email('[email protected]')) # print(check_email('[email protected]')) # logger.info(check_email('[email protected]')) # logger.info(check_email('[email protected]')) # logger.info(check_email('[email protected]')) # logger.info(check_email('[email protected]')) # logger.info(check_email('[email protected]')) # logger.info(check_email('[email protected]')) # print(check_email('[email protected]')) # print(check_email('[email protected]', detailed_result=True))
счсчсчсч чсчсч чс чсEnter