Spaces:
Sleeping
Sleeping
import gradio as gr | |
import re | |
import dns.resolver | |
from typing import Tuple | |
def check_syntax(mail_address: str) -> Tuple[bool, str]: | |
"""μ΄λ©μΌ μ£Όμ ꡬ문 κ²μ¬""" | |
# λ μ νν μ΄λ©μΌ μ κ·μ ν¨ν΄ | |
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' | |
match = re.match(pattern, mail_address) | |
if match: | |
return True, "β μ΄λ©μΌ μ£Όμ νμμ΄ μ¬λ°λ¦ λλ€." | |
return False, "β μ΄λ©μΌ μ£Όμ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€." | |
def check_dns(domain: str) -> Tuple[bool, str]: | |
"""DNS MX λ μ½λ κ²μ¬""" | |
try: | |
records = dns.resolver.resolve(domain, 'MX') # query λμ resolve μ¬μ© | |
if records: | |
return True, "β λλ©μΈμ λ©μΌ μλ²κ° μ‘΄μ¬ν©λλ€." | |
return False, "β λλ©μΈμ λ©μΌ μλ²λ₯Ό μ°Ύμ μ μμ΅λλ€." | |
except Exception as e: | |
return False, f"β DNS νμΈ μ€ μ€λ₯ λ°μ: {str(e)}" | |
def validate_email(mail_address: str) -> str: | |
"""μ΄λ©μΌ μ£Όμ μ’ ν© κ²μ¦""" | |
results = [] | |
# 1. ꡬ문 κ²μ¬ | |
syntax_valid, syntax_msg = check_syntax(mail_address) | |
results.append(syntax_msg) | |
if not syntax_valid: | |
return "\n".join(results) | |
# 2. λλ©μΈ μΆμΆ λ° DNS κ²μ¬ | |
try: | |
domain = mail_address.split('@')[1] | |
dns_valid, dns_msg = check_dns(domain) | |
results.append(dns_msg) | |
if not dns_valid: | |
return "\n".join(results) | |
# κ²μ¦ μ±κ³΅ | |
results.append("β μ΄λ©μΌ μ£Όμκ° μ ν¨ν©λλ€.") | |
except Exception as e: | |
results.append(f"β κ²μ¦ μ€ μ€λ₯ λ°μ: {str(e)}") | |
return "\n".join(results) | |
# Gradio μΈν°νμ΄μ€ κ΅¬μ± | |
iface = gr.Interface( | |
fn=validate_email, | |
inputs=gr.Textbox( | |
label="μ΄λ©μΌ μ£Όμλ₯Ό μ λ ₯νμΈμ", | |
placeholder="[email protected]" | |
), | |
outputs=gr.Textbox(label="κ²μ¦ κ²°κ³Ό"), | |
title="μ΄λ©μΌ μ£Όμ κ²μ¦ λꡬ", | |
description=""" | |
μ΄ λꡬλ λ€μ μ¬νλ€μ κ²μ¦ν©λλ€: | |
1. μ΄λ©μΌ μ£Όμ νμ κ²μ¬ | |
2. λλ©μΈμ λ©μΌ μλ²(MX λ μ½λ) μ‘΄μ¬ μ¬λΆ νμΈ | |
""", | |
examples=[ | |
["[email protected]"], | |
["[email protected]"], | |
["[email protected]"], | |
["malformed@@email.com"] | |
], | |
theme=gr.themes.Soft() # λΆλλ¬μ΄ ν λ§ μ μ© | |
) | |
# μ ν리μΌμ΄μ μ€ν | |
if __name__ == "__main__": | |
iface.launch() |