return from function

Emil Natan shlyoko at gmail.com
Sun Dec 20 07:34:40 EST 2015


I'm completely new to Python.
I have the following function to find the parent for domain. It removes the
left most label from the name and then checks if SOA record exists for the
reminder, if not it calls itself recursively removing another label and
checking again for SOA record. It works well for 'co.uk' and 'amazon.co.uk'
for example. It does not return the expected value 'uk' when invoked for '
amazon1.co1.uk', though the print command before the return prints what is
expected. Can someone explain why? Thanks.

>>> find_parent_domain('amazon.co1.uk.')
Test for parent domain co1.uk.
NXDOMAIN: invoke find_parent_domain recursively
Test for parent domain uk.
the parent domain we use is: uk.
>>>

import dns.resolver
from dns.exception import DNSException

def find_parent_domain(domainname):

    if domainname == '.':
        parent_domain = None
        return parent_domain

    parent_domain = domainname.partition('.')[2]
    try:
        print('Test for parent domain %s' % parent_domain)
        z = dns.resolver.query(parent_domain, 'SOA')
        print('the parent domain we use is: %s' % parent_domain)
        return parent_domain
    except dns.resolver.NXDOMAIN:
        print('NXDOMAIN: invoke find_parent_domain recursively')
        find_parent_domain(parent_domain)
    except dns.resolver.NoAnswer:
        print('NoAnswer: invoke find_parent_domain recursively')
        find_parent_domain(parent_domain)



More information about the Python-list mailing list