-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.py
114 lines (94 loc) · 4.67 KB
/
validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import tkinter as tk
from tkinter import filedialog
import openpyxl
import smtplib
import dns.resolver
def validate_email(email):
# Проверяем наличие символа "@" в адресе электронной почты
if '@' not in email:
return False
# Разделяем адрес электронной почты для извлечения домена
domain = email.split('@')[1]
try:
# Запрашиваем MX-записи домена
records = dns.resolver.resolve(domain, 'MX')
mx_record = str(records[0].exchange)
# Подключаемся к SMTP-серверу домена
server = smtplib.SMTP()
server.set_debuglevel(0)
server.connect(mx_record)
server.helo(server.local_hostname)
server.mail('[email protected]')
# Проверяем код ответа для адреса электронной почты
code, message = server.rcpt(str(email))
server.quit()
# Если код ответа равен 250, адрес электронной почты действителен
if code == 250:
return True
else:
return False
except dns.resolver.NXDOMAIN:
return False
except smtplib.SMTPConnectError:
return False
except smtplib.SMTPServerDisconnected:
return False
except smtplib.SMTPResponseException:
return False
except:
return False
def validate_emails():
# Открываем диалоговое окно для выбора файла Excel
filepath = filedialog.askopenfilename(title="Выберите файл Excel",
filetypes=(("Файлы Excel", "*.xlsx"),
("Все файлы", "*.*")))
if filepath:
# Загружаем файл Excel
wb = openpyxl.load_workbook(filepath)
sheet = wb.active
# Создаем новую книгу Excel для сохранения действительных адресов электронной почты
new_wb = openpyxl.Workbook()
new_sheet = new_wb.active
valid_count = 0
row_index = 1
# Перебираем строки в файле Excel
for row in sheet.iter_rows(values_only=True):
email = row[0]
if email and validate_email(email):
# Если адрес электронной почты действителен, записываем его в новую книгу
new_sheet.cell(row=row_index, column=1, value=email)
valid_count += 1
row_index += 1
counter_value.set("Проверено: {}/{}".format(row_index-1, sheet.max_row-1))
window.update()
# Сохраняем новую книгу с действительными адресами электронной почты
new_filepath = filedialog.asksaveasfilename(
title="Сохранить действительные адреса электронной почты",
defaultextension=".xlsx",
filetypes=(("Файлы Excel", "*.xlsx"), ("Все файлы", "*.*")))
if new_filepath:
new_wb.save(new_filepath)
# Обновляем метку счетчика
counter_value.set("Действительные адреса электронной почты: {}".format(valid_count))
window.update()
# Создаем окно Tkinter
window = tk.Tk()
window.title("Валидатор адресов электронной почты")
# Задаем размер и положение окна
window_width = 330
window_height = 130
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = int((screen_width / 2) - (window_width / 2))
y = int((screen_height / 2) - (window_height / 2))
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
# Создаем кнопку для запуска процесса валидации адресов электронной почты
validate_button = tk.Button(window, text="Проверить адреса электронной почты",
command=validate_emails)
validate_button.pack(pady=10)
# Создаем метку для отображения счетчика
counter_value = tk.StringVar()
counter_label = tk.Label(window, textvariable=counter_value)
counter_label.pack()
# Запускаем цикл событий Tkinter
window.mainloop()