seawolf2357 commited on
Commit
2ec2072
ยท
verified ยท
1 Parent(s): 167ca7c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import re
3
+ import dns.resolver
4
+ import socket
5
+ import smtplib
6
+ from typing import Tuple, Union
7
+
8
+ def check_syntax(mail_address: str) -> bool:
9
+ """์ด๋ฉ”์ผ ์ฃผ์†Œ ๊ตฌ๋ฌธ ๊ฒ€์‚ฌ"""
10
+ match = re.match('[A-Za-z0-9._+]+@[A-Za-z]+.[A-Za-z]', mail_address)
11
+ return match is not None
12
+
13
+ def check_dns(domain: str) -> Tuple[bool, str]:
14
+ """DNS MX ๋ ˆ์ฝ”๋“œ ๊ฒ€์‚ฌ"""
15
+ try:
16
+ records = dns.resolver.query(domain, 'MX')
17
+ mx_record = str(records[0].exchange)
18
+ return True, mx_record
19
+ except Exception as e:
20
+ return False, str(e)
21
+
22
+ def check_smtp(mail_address: str, mx_record: str) -> bool:
23
+ """SMTP ์„œ๋ฒ„ ์—ฐ๊ฒฐ ๊ฒ€์‚ฌ"""
24
+ try:
25
+ local_host = socket.gethostname()
26
+ server = smtplib.SMTP(timeout=10)
27
+ server.set_debuglevel(0)
28
+ server.connect(mx_record)
29
+ server.helo(local_host)
30
+ server.mail('[email protected]')
31
+ code, message = server.rcpt(str(mail_address))
32
+ server.quit()
33
+ return code == 250
34
+ except Exception:
35
+ return False
36
+
37
+ def validate_email(mail_address: str) -> str:
38
+ """์ด๋ฉ”์ผ ์ฃผ์†Œ ์ข…ํ•ฉ ๊ฒ€์ฆ"""
39
+ # ๊ฒฐ๊ณผ ๋ฉ”์‹œ์ง€ ์ดˆ๊ธฐํ™”
40
+ result_messages = []
41
+
42
+ # 1. ๊ตฌ๋ฌธ ๊ฒ€์‚ฌ
43
+ if not check_syntax(mail_address):
44
+ return "โŒ ์ด๋ฉ”์ผ ์ฃผ์†Œ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
45
+ result_messages.append("โœ… ์ด๋ฉ”์ผ ์ฃผ์†Œ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฆ…๋‹ˆ๋‹ค.")
46
+
47
+ # 2. ๋„๋ฉ”์ธ ์ถ”์ถœ
48
+ domain = mail_address.split('@')[1]
49
+
50
+ # 3. DNS MX ๋ ˆ์ฝ”๋“œ ๊ฒ€์‚ฌ
51
+ dns_valid, mx_record = check_dns(domain)
52
+ if not dns_valid:
53
+ return "\n".join(result_messages + ["โŒ ๋„๋ฉ”์ธ์˜ ๋ฉ”์ผ ์„œ๋ฒ„๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."])
54
+ result_messages.append("โœ… ๋„๋ฉ”์ธ์˜ ๋ฉ”์ผ ์„œ๋ฒ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.")
55
+
56
+ # 4. SMTP ๊ฒ€์‚ฌ
57
+ if not check_smtp(mail_address, mx_record):
58
+ return "\n".join(result_messages + ["โŒ ์ด๋ฉ”์ผ ์ฃผ์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."])
59
+ result_messages.append("โœ… ์ด๋ฉ”์ผ ์ฃผ์†Œ๊ฐ€ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.")
60
+
61
+ return "\n".join(result_messages)
62
+
63
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
64
+ iface = gr.Interface(
65
+ fn=validate_email,
66
+ inputs=gr.Textbox(label="์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”", placeholder="[email protected]"),
67
+ outputs=gr.Textbox(label="๊ฒ€์ฆ ๊ฒฐ๊ณผ"),
68
+ title="์ด๋ฉ”์ผ ์ฃผ์†Œ ๊ฒ€์ฆ ๋„๊ตฌ",
69
+ description="์ž…๋ ฅ๋œ ์ด๋ฉ”์ผ ์ฃผ์†Œ์˜ ์œ ํšจ์„ฑ์„ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์— ๊ฑธ์ณ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.",
70
+ examples=[
71
72
73
+ ["malformed@@email.com"]
74
+ ]
75
+ )
76
+
77
+ # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
78
+ if __name__ == "__main__":
79
+ iface.launch()