ok, this seems like it should be really simple but Im a bit confused:
i have two values – domain and ip
its best described with code:
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)
so this function checks the domain whois, if it doesnt find it, its tries a whois with the ip address, but what I want to know is what is the best way to check if the domain is equal to None dont bother with the domain check and go onto the ip check, yet also if the domain is NOT equal to None, do the domain check and then the ip check in that order. Sorry if this is basic, this has me a bit confused. I guess i could set a variable and test for that but is there a more elegant way of doing it ?
if i put at the top a
if domain != None:
the only way it seems i can do it is by repeating the code or setting a variable, I guess there must be other conditional tests I could use that I dont know about.
EDIT: update 2 based on answers below: – ive also put in the country checking code with my database.
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))
part of the problem with making this robust is the varying values returned by the whois server eg for this IP: 67.222.137.216
the whois server returns :
@ : 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
#
thanks for any help.
Try this