首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的初学者内容,多条件测试

问简单的初学者内容,多条件测试
EN

Stack Overflow用户
提问于 2011-11-28 23:23:11
回答 3查看 169关注 0票数 1

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

我有两个值- domain和ip,它最好用代码来描述:

代码语言:javascript
复制
        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)

所以这个函数检查谁是域,如果没有找到它,它会尝试使用ip地址,但我想知道的是,检查域是否等于None的最好方法是什么?不要费心域检查,然后进行ip检查,如果域不等于None,则按该顺序执行域检查,然后进行ip检查。如果这是基本的,我很抱歉,这让我有点困惑。我想我可以设置一个变量并对其进行测试,但是有没有更优雅的方法呢?

如果我在顶部加上一个

代码语言:javascript
复制
if domain != None:

我唯一能做的似乎是通过重复代码或设置一个变量,我猜肯定还有其他我不知道的条件测试可以使用。

编辑:更新2基于以下答案:-我也在国家和我的数据库检查代码。

代码语言:javascript
复制
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服务器返回:

代码语言:javascript
复制
@ : 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
#

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-28 23:43:53

尝尝这个

代码语言:javascript
复制
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))
票数 1
EN

Stack Overflow用户

发布于 2011-11-28 23:28:14

这是行不通的:

代码语言:javascript
复制
if 'Not found' or 'No entries' in whois_result:

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

回答你的问题:

代码语言:javascript
复制
if domain != None && domain != 1:

-编辑-

如果你的意思是None而不是“1”,你应该简单地将你的IP检查代码(将在域检查之后执行)放在与域检查相同的缩进级别。

票数 0
EN

Stack Overflow用户

发布于 2011-11-28 23:28:42

这个条件是错误的:

代码语言:javascript
复制
if 'Not found' or 'No entries' in whois_result:

因为表达式'Not found' or 'No entries' in whois_result总是返回'Not found',所以它的计算结果总是"true“。

您需要将if-语句更改为:

代码语言:javascript
复制
if 'Not found' in whois_result or 'No entries' in whois_result:
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8298043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档