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('johndoe@example.com')
<DNS name johndoe.example.com.>
>>> email_to_rname('john.doe@example.com')
<DNS name john\.doe.example.com.>
>>> print email_to_rname('johndoe@example.com')
johndoe.example.com.
>>> print email_to_rname('john.doe@example.com')
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.'))
'johndoe@example.com'
>>> rname_to_email(from_text('john\\.doe.example.com.'))
'john.doe@example.com'
>>> rname_to_email(email_to_rname('johndoe@example.com'))
'johndoe@example.com'
>>> rname_to_email(email_to_rname('john.doe@example.com'))
'john.doe@example.com'
"""
labels = list(rname)
username, domain = labels[0], '.'.join(labels[1:]).rstrip('.')
username = username.replace('\\.', '.') # unescape . in username
return '@'.join((username, domain))