是否存在用于将电子邮件地址映射到 SOA 记录的 RNAME 字段(及其逆)的现有/标准算法?我正在使用dnspython包,但我在他们的源代码树中没有看到任何东西来处理这个问题。我遇到了有句号“.”的边缘情况。在需要转义的用户名中,想知道是否还有我遗漏的其他边缘情况。 RFC 1035简单地说:
A <domain-name> which specifies the mailbox of the person responsible for this zone.
除了 RFC 1183 中的简短提及外,更新 1035 的 RFC 均未扩展 RNAME 字段。 .
最佳答案
这是我使用 dnspython 得出的结果:
from dns.name import from_text
def email_to_rname(email):
"""Convert standard email address into RNAME field for SOA record.
>>> email_to_rname('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfb5b0b7b1bbb0ba9fbaa7beb2afb3baf1bcb0b2" rel="noreferrer noopener nofollow">[email protected]</a>')
<DNS name johndoe.example.com.>
>>> email_to_rname('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7ada8afa9e9a3a8a287a2bfa6aab7aba2e9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a>')
<DNS name john\.doe.example.com.>
>>> print email_to_rname('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f59f9a9d9b919a90b5908d9498859990db969a98" rel="noreferrer noopener nofollow">[email protected]</a>')
johndoe.example.com.
>>> print email_to_rname('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b11141315551f141e3b1e031a160b171e55181416" rel="noreferrer noopener nofollow">[email protected]</a>')
john\.doe.example.com.
"""
username, domain = email.split('@', 1)
username = username.replace('.', '\\.') # escape . in username
return from_text('.'.join((username, domain)))
def rname_to_email(rname):
"""Convert SOA record RNAME field into standard email address.
>>> rname_to_email(from_text('johndoe.example.com.'))
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="49232621272d262c092c31282439252c672a2624" rel="noreferrer noopener nofollow">[email protected]</a>'
>>> rname_to_email(from_text('john\\.doe.example.com.'))
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9af0f5f2f4b4fef5ffdaffe2fbf7eaf6ffb4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>'
>>> rname_to_email(email_to_rname('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13797c7b7d777c7653766b727e637f763d707c7e" rel="noreferrer noopener nofollow">[email protected]</a>'))
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22484d4a4c464d4762475a434f524e470c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>'
>>> rname_to_email(email_to_rname('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a40454244044e454f6a4f524b475a464f04494547" rel="noreferrer noopener nofollow">[email protected]</a>'))
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f79d989f99d9939892b7928f969a879b92d994989a" rel="noreferrer noopener nofollow">[email protected]</a>'
"""
labels = list(rname)
username, domain = labels[0], '.'.join(labels[1:]).rstrip('.')
username = username.replace('\\.', '.') # unescape . in username
return '@'.join((username, domain))
关于python - 如何将电子邮件地址映射到 SPA RENAME 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044852/