如何在 python 中验证域的 MX 记录?

发布于 2024-07-14 07:32:09 字数 365 浏览 3 评论 0原文

我有大量电子邮件地址需要验证。 最初我用正则表达式解析它们以抛出完全疯狂的那些。 我留下了那些看起来合理但仍然可能包含错误的内容。

我想查找哪些地址具有有效域,因此给定 [email protected]我想知道是否可以向 abcxyz.com 发送电子邮件。

我想测试一下它是否对应于有效的 A 或 MX 记录 - 有没有一种简单的方法可以仅使用 Python 标准库来做到这一点? 我不想只是为了支持此功能而向我的项目添加额外的依赖项。

I have a large number of email addresses to validate. Initially I parse them with a regexp to throw out the completely crazy ones. I'm left with the ones that look sensible but still might contain errors.

I want to find which addresses have valid domains, so given [email protected] I want to know if it's even possible to send emails to abcxyz.com .

I want to test that to see if it corresponds to a valid A or MX record - is there an easy way to do it using only Python standard library? I'd rather not add an additional dependency to my project just to support this feature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡渣熟男 2024-07-21 07:32:09

标准库中没有 DNS 接口,因此您必须自行构建或使用第三方库。

但这不是一个快速变化的概念,因此外部库很稳定并且经过了良好的测试。

我成功地用于与您的问题相同的任务的是 PyDNS

我的代码的一个非常粗略的草图是这样的:

import DNS, smtplib

DNS.DiscoverNameServers()
mx_hosts = DNS.mxlookup(hostname)

# Just doing the mxlookup might be enough for you,
# but do something like this to test for SMTP server
for mx in mx_hosts:
    smtp = smtplib.SMTP()
    #.. if this doesn't raise an exception it is a valid MX host...
    try:
        smtp.connect(mx[1])
    except smtplib.SMTPConnectError:
        continue # try the next MX server in list

另一个可能比 PyDNS 更好/更快的库是 dnsmodule 尽管与 PyDNS 2008 年 8 月的最后一次更新相比,它看起来自 2002 年以来就没有任何活动。

编辑:我还想指出,电子邮件地址不能使用正则表达式可以轻松解析。 您最好使用标准库 email.utils 模块中的 parseaddr() 函数(请参阅我的 回答这个问题)。

There is no DNS interface in the standard library so you will either have to roll your own or use a third party library.

This is not a fast-changing concept though, so the external libraries are stable and well tested.

The one I've used successful for the same task as your question is PyDNS.

A very rough sketch of my code is something like this:

import DNS, smtplib

DNS.DiscoverNameServers()
mx_hosts = DNS.mxlookup(hostname)

# Just doing the mxlookup might be enough for you,
# but do something like this to test for SMTP server
for mx in mx_hosts:
    smtp = smtplib.SMTP()
    #.. if this doesn't raise an exception it is a valid MX host...
    try:
        smtp.connect(mx[1])
    except smtplib.SMTPConnectError:
        continue # try the next MX server in list

Another library that might be better/faster than PyDNS is dnsmodule although it looks like it hasn't had any activity since 2002, compared to PyDNS last update in August 2008.

Edit: I would also like to point out that email addresses can't be easily parsed with a regexp. You are better off using the parseaddr() function in the standard library email.utils module (see my answer to this question for example).

单身狗的梦 2024-07-21 07:32:09

不在标准库中执行此操作的简单方法是使用 validate_email package:

from validate_email import validate_email
is_valid = validate_email('[email protected]', check_mx=True)

为了更快地处理大量电子邮件地址(例如列出电子邮件),您可以隐藏域名,并且仅在域名不存在时才执行 check_mx。例如:

emails = ["[email protected]", "email@bad_domain", "[email protected]", ...]
verified_domains = set()
for email in emails:
    domain = email.split("@")[-1]
    domain_verified = domain in verified_domains
    is_valid = validate_email(email, check_mx=not domain_verified)
    if is_valid:
        verified_domains.add(domain)

The easy way to do this NOT in the standard library is to use the validate_email package:

from validate_email import validate_email
is_valid = validate_email('[email protected]', check_mx=True)

For faster results to process a large number of email addresses (e.g. list emails, you could stash the domains and only do a check_mx if the domain isn't there. Something like:

emails = ["[email protected]", "email@bad_domain", "[email protected]", ...]
verified_domains = set()
for email in emails:
    domain = email.split("@")[-1]
    domain_verified = domain in verified_domains
    is_valid = validate_email(email, check_mx=not domain_verified)
    if is_valid:
        verified_domains.add(domain)
故事和酒 2024-07-21 07:32:09

一种简单有效的方法是使用名为 validate_email 的 python 包。
该软件包提供了这两种功能。 查看这篇文章,它将帮助您检查您的电子邮件是否确实存在

An easy and effective way is to use a python package named as validate_email.
This package provides both the facilities. Check this article which will help you to check if your email actually exists or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文