Spaces:
Sleeping
Sleeping
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() |