Python 简单的初学者资料,多条件测试

Python 简单的初学者资料,多条件测试,python,conditional,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: pri

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

我有两个值-域和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
#

谢谢您的帮助。

这行不通:

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

回答你的问题:

if domain != None && domain != 1:
-编辑-


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

这不起作用:

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

回答你的问题:

if domain != None && domain != 1:
-编辑-


如果您的意思是
None
而不是“one”,则只需将IP检查代码(将在域检查后执行)与域检查放在相同的缩进级别上即可。

此条件错误:

if 'Not found' or 'No entries' in whois_result:
它将始终计算为“true”,因为whois\u结果中的表达式
'Not found'或'No entries'将始终返回
'Not found'

您需要将if语句更改为:

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

这种情况是错误的:

if 'Not found' or 'No entries' in whois_result:
它将始终计算为“true”,因为whois\u结果中的表达式
'Not found'或'No entries'将始终返回
'Not found'

您需要将if语句更改为:

if 'Not found' in whois_result or 'No entries' in whois_result:
试试这个

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

def check_whois(data):
    if 'No entries' in data or 'Not found' in data:return False
    test = re.search("country.+([A-Z].)",whois_result)
    return test.group(1)

def find_whois(domain, ip):
    return check_whois(do_whois(domain)) or check_whois(do_whois(ip))
试试这个

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

def check_whois(data):
    if 'No entries' in data or 'Not found' in data:return False
    test = re.search("country.+([A-Z].)",whois_result)
    return test.group(1)

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

谢谢,伙计们,啊,我明白了-是的,我有点猜测,希望它能起作用,因为它没有窒息,我认为它是有效的。哦,是的,现在变得更清楚了。谢谢。哦,这真是个愚蠢的问题,我想我已经盯着这个问题看太久了:)非常抱歉。但是谢谢你帮我整理了这篇声明。@离开电脑,呼吸点新鲜空气,散散步,或者干脆把它放在一边,直到明天。然后,当您回到代码时,您可能会发现答案是“doh”时刻一直都是这样:)-当我弹吉他的时候我也弹不到它-离开一天,回来,这看起来很容易:)事情是这样的,我现在沉迷于这件事,我必须完成它!谢谢,伙计们,啊,我明白了-是的,我有点猜测,希望它能起作用,因为它没有窒息,我认为它是有效的。哦,是的,现在变得更清楚了。谢谢。哦,这真是个愚蠢的问题,我想我已经盯着这个问题看太久了:)非常抱歉。但是谢谢你帮我整理了这篇声明。@离开电脑,呼吸点新鲜空气,散散步,或者干脆把它放在一边,直到明天。然后,当您回到代码时,您可能会发现答案是“doh”时刻一直都是这样:)-当我弹吉他的时候我也弹不到它-离开一天,回来,这看起来很容易:)事情是这样的,我现在沉迷于这件事,我必须完成它@雪茄嘿,这样的事情会发生的!:)删除了我答案中的这一部分。@cigar-Heh,这样的事情会发生的!:)删除了我答案中的这一部分。哇,好吧,这是一种新的看待它的方式,所以如果函数从find\u whois返回False,它默认为check\u whois?谢谢你!上面是什么?我改变了一些事情。例如,whois\u结果应该是check\u whois中的数据,我认为do\u whois中的if应该不同。不确定。哇,好吧,这是一种新的看待它的方式,所以如果函数从find_whois返回False,它默认为check_whois?谢谢你!上面是什么?我改变了一些事情。例如,whois\u结果应该是check\u whois中的数据,我认为do\u whois中的if应该不同。不确定。与
None
相比,通常使用
is
is not
而不是
=
=。这是因为只有一个
None
值,所以测试身份比测试相等更好。啊,好的,我会记住,谢谢:)当与
None
比较时,习惯上使用
is
is not
代替
=
=。这是因为只有一个
None
值,所以测试身份比测试相等更好。啊,好的,我会记住的,谢谢:)