Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Emil Natan Newsgroups: comp.lang.python Subject: return from function Date: Sun, 20 Dec 2015 12:34:40 +0000 Lines: 37 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de /fxfPfEFsZrkjVW1DoP6Bwd9YJLV3hWxh103jH3m8J7Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'removes': 0.05; "%s'": 0.09; 'expected.': 0.09; 'record.': 0.09; 'python.': 0.11; 'def': 0.13; '>>>': 0.15; 'skip:f 30': 0.15; 'received:io': 0.16; 'received:psf.io': 0.16; 'example.': 0.18; 'exists': 0.18; 'try:': 0.18; 'thanks.': 0.18; '>>>': 0.20; 'import': 0.24; 'command': 0.26; 'checking': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:f 40': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'record': 0.29; 'invoke': 0.29; 'prints': 0.29; "i'm": 0.30; 'print': 0.30; 'checks': 0.30; 'another': 0.32; 'except': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'label': 0.35; 'received:74.125.82': 0.35; 'expected': 0.35; 'to:addr:python- list': 0.36; 'itself': 0.38; 'someone': 0.38; 'test': 0.39; 'does': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'url:co': 0.65; 'received:74.125.82.41': 0.84; 'reminder,': 0.84; 'why?': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=mZx2Vte/5IR3TdCjiyWO0NfUWld4yTC1WKl7KAGRexg=; b=oD/tXptfD34PGTQwtRmoQwo42s+wRFoz+VYzEoFmNSU92CGzLlU9o73SVK2+2v0Bhj jhaTbEyeBbKGSrjVwxRhoqUqaauQfhFzsK3WmPLv9pcm0keEsE5KuCE2O1C594/XP2wC qDawwABhASsbsoVJ7vEA1vG6qJpv972+l55COtV8rpOzf0ZSlgGB4FQtcu9J5NYmj1lz w8gHv8pSksBAWNyMPdGo7AOgEITzefIEZ40D15Bp22zVQ4CiEbi+GmM5vJYI6dbPTZTq 8R24kMHQ8UEuEtz2eIbmhC8J5LhzhbBJYcbkQLqCXSoE/oGjqMNWuQsIplEii51SbWZL rIAA== X-Received: by 10.194.95.199 with SMTP id dm7mr14610592wjb.15.1450614880582; Sun, 20 Dec 2015 04:34:40 -0800 (PST) X-Mailman-Approved-At: Mon, 21 Dec 2015 07:01:11 -0500 X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100662 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)