简单的初学者的东西,多个条件测试

时间:2011-11-28 15:23:11

标签: python conditional

好吧,这看起来应该很简单,但我有点困惑:

我有两个值 - 域名和IP 最好用代码描述:

        whois_result = Popen(['whois', str(domain)], stdout=PIPE,stderr=STDOUT).communicate()[0]
        debug_output(whois_result)
        if 'Not found' or 'No entries' in whois_result:
                print "Processing whois failure on '%s'" % str(domain)
                print "Trying the IP (%s) instead..." % ip
                whois_result = Popen(['whois', ip], stdout=PIPE,stderr=STDOUT).communicate()[0]
                debug_output(whois_result)
                if 'Not found' or 'No entries' in whois_result:
                        print "complete and utter whois failure, its you isnt it, not me."
                        return False
                else:
                        test = re.search("country.+([A-Z].)",whois_result)
                        countryid = test.group(1)

所以这个函数检查域whois,如果它找不到它,它尝试使用ip地址的whois,但我想知道的是检查域是否等于None的最佳方法是什么不要打扰域检查并进入ip检查,但如果域不等于None,请进行域检查,然后按顺序进行ip检查。对不起,如果这是基本的,这让我有点困惑。我想我可以设置一个变量并测试它,但有更优雅的方式吗?

如果我放在顶部

if domain != None:

我能做到的唯一方法就是重复代码或设置变量,我想必须有其他我可以使用的条件测试,我不知道。

编辑:根据以下答案更新2: - 我还在国家/地区使用我的数据库检查代码。

def do_whois(data):
        if data is not None: return Popen(['whois', str(data)], stdout=PIPE,stderr=STDOUT).communicate()[0]
        return 'No entries'

def check_whois(data):
        conn = sqlite3.connect(db_name)
        cursor = conn.cursor()

        if 'No entries' in data or 'Not found' in data or 'No match for' in data:return False
        id = re.search("country.+([A-Z].)",data)
        if id is None:
                print "we didnt get nuttin from whois"
                return False
        countryid = id.group(1) 
        # fetch country from countrycode db
        cursor.execute("SELECT country,countrycode FROM countries WHERE countrycode = ?",(countryid,))
        country = cursor.fetchone()
        country = country[0]
        print "Server is from: " + country
        return (country,countryid)       

def find_country(domain, ip):
        return check_whois(do_whois(domain)) or check_whois(do_whois(ip))

使这个健壮的问题的一部分是whois服务器返回的变化值,例如对于这个IP:67.222.137.216

whois服务器返回:

@ : whois 67.222.137.216
#
# Query terms are ambiguous.  The query is assumed to be:
#     "n 67.222.137.216"
#
# Use "?" to get help.
#

#
# The following results may also be obtained via:
# http://whois.arin.net/rest/nets;q=67.222.137.216?showDetails=true&showARIN=false&ext=netref2
#

BLUESQUAREDATAVPSLLC BLUESQUAREDATAVPSLLCNET (NET-67-222-137-213-1) 67.222.137.213 - 67.222.137.244
DFW Datacenter DFW-DATACENTER (NET-67-222-128-0-1) 67.222.128.0 - 67.222.159.255


#
# ARIN WHOIS data and services are subject to the Terms of Use
# available at: https://www.arin.net/whois_tou.html
#

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这不起作用:

if 'Not found' or 'No entries' in whois_result:

这被解释为if ('Not found') or ('No entries' in whois_result),它总是返回True。

回答你的问题:

if domain != None && domain != 1:

-edit -

如果您的意思是None而不是“一”,那么您应该将您的IP检查代码(在域检查后执行)放在与域检查相同的缩进级别上。

答案 1 :(得分:0)

这种情况是错误的:

if 'Not found' or 'No entries' in whois_result:

它始终会评估为“true”,因为表达式'Not found' or 'No entries' in whois_result将始终返回'Not found'

您需要将if语句更改为:

if 'Not found' in whois_result or 'No entries' in whois_result: