Spaces:
Sleeping
Sleeping
File size: 2,681 Bytes
2ec2072 |
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 |
import gradio as gr
import re
import dns.resolver
import socket
import smtplib
from typing import Tuple, Union
def check_syntax(mail_address: str) -> bool:
"""์ด๋ฉ์ผ ์ฃผ์ ๊ตฌ๋ฌธ ๊ฒ์ฌ"""
match = re.match('[A-Za-z0-9._+]+@[A-Za-z]+.[A-Za-z]', mail_address)
return match is not None
def check_dns(domain: str) -> Tuple[bool, str]:
"""DNS MX ๋ ์ฝ๋ ๊ฒ์ฌ"""
try:
records = dns.resolver.query(domain, 'MX')
mx_record = str(records[0].exchange)
return True, mx_record
except Exception as e:
return False, str(e)
def check_smtp(mail_address: str, mx_record: str) -> bool:
"""SMTP ์๋ฒ ์ฐ๊ฒฐ ๊ฒ์ฌ"""
try:
local_host = socket.gethostname()
server = smtplib.SMTP(timeout=10)
server.set_debuglevel(0)
server.connect(mx_record)
server.helo(local_host)
server.mail('[email protected]')
code, message = server.rcpt(str(mail_address))
server.quit()
return code == 250
except Exception:
return False
def validate_email(mail_address: str) -> str:
"""์ด๋ฉ์ผ ์ฃผ์ ์ข
ํฉ ๊ฒ์ฆ"""
# ๊ฒฐ๊ณผ ๋ฉ์์ง ์ด๊ธฐํ
result_messages = []
# 1. ๊ตฌ๋ฌธ ๊ฒ์ฌ
if not check_syntax(mail_address):
return "โ ์ด๋ฉ์ผ ์ฃผ์ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค."
result_messages.append("โ
์ด๋ฉ์ผ ์ฃผ์ ํ์์ด ์ฌ๋ฐ๋ฆ
๋๋ค.")
# 2. ๋๋ฉ์ธ ์ถ์ถ
domain = mail_address.split('@')[1]
# 3. DNS MX ๋ ์ฝ๋ ๊ฒ์ฌ
dns_valid, mx_record = check_dns(domain)
if not dns_valid:
return "\n".join(result_messages + ["โ ๋๋ฉ์ธ์ ๋ฉ์ผ ์๋ฒ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."])
result_messages.append("โ
๋๋ฉ์ธ์ ๋ฉ์ผ ์๋ฒ๊ฐ ์กด์ฌํฉ๋๋ค.")
# 4. SMTP ๊ฒ์ฌ
if not check_smtp(mail_address, mx_record):
return "\n".join(result_messages + ["โ ์ด๋ฉ์ผ ์ฃผ์๊ฐ ์กด์ฌํ์ง ์๊ฑฐ๋ ํ์ธํ ์ ์์ต๋๋ค."])
result_messages.append("โ
์ด๋ฉ์ผ ์ฃผ์๊ฐ ์ ํจํฉ๋๋ค.")
return "\n".join(result_messages)
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
iface = gr.Interface(
fn=validate_email,
inputs=gr.Textbox(label="์ด๋ฉ์ผ ์ฃผ์๋ฅผ ์
๋ ฅํ์ธ์", placeholder="[email protected]"),
outputs=gr.Textbox(label="๊ฒ์ฆ ๊ฒฐ๊ณผ"),
title="์ด๋ฉ์ผ ์ฃผ์ ๊ฒ์ฆ ๋๊ตฌ",
description="์
๋ ฅ๋ ์ด๋ฉ์ผ ์ฃผ์์ ์ ํจ์ฑ์ ์ฌ๋ฌ ๋จ๊ณ์ ๊ฑธ์ณ ๊ฒ์ฆํฉ๋๋ค.",
examples=[
["[email protected]"],
["[email protected]"],
["malformed@@email.com"]
]
)
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
if __name__ == "__main__":
iface.launch() |