python - re.split return None in function but normaly works OK -


def whois(k,  i):     k = str(k[i])     print (k)     whois = subprocess.popen(['whois',  k], stdout=subprocess.pipe, stderr=subprocess.pipe)     ou, err = whois.communicate()     = str(ou)     print (who.find('netname:'))     = re.split('netname:',  who)[1]     = re.split('nethandle',   who)[0]     = who.replace(r'\n', '')      print (whois(k,  4)) 

output:

108.160.172.204  520  none 

why python return "none" not "dropbox"? if take code and:

k = '108.160.172.204' print (k) whois = subprocess.popen(['whois',  k], stdout=subprocess.pipe, stderr=subprocess.pipe) ou, err = whois.communicate() = str(ou) print (who.find('netname:')) = re.split('netname:',  who)[1] = re.split('nethandle',   who)[0] = who.replace(r'\n', '') print (who) 

output :

108.160.172.204  520  dropbox 

your function not return value returns none python functions don't specify return value do:

def whois(k,  i):     k = str(k[i])     print (k)     whois = subprocess.popen(['whois',  k], stdout=subprocess.pipe, stderr=subprocess.pipe)     ou, err = whois.communicate()     = str(ou)     print (who.find('netname:'))     = re.split('netname:',  who)[1]     = re.split('nethandle',   who)[0]     = who.replace(r'\n', '')      return # return 

you can use check_output if want output, don't need re split:

from subprocess import check_output def whois(k,  i):     k = str(k[i])     = check_output(['whois',  k], universal_newlines=true)     = who.split('netname:')[1]     = who.split('nethandle')[0]     return  who.replace(r'\n', '') 

there multiple python libraries want couple of ipwhois, python-whois

if want nethandle can use re find that:

def whois(k,  i):     k = k[i]     = check_output(['whois',  k], universal_newlines=true)     return  dict(map(str.strip,ele.split(":",1)) ele in re.findall(r'^\w+:\s+.*', who, re.m)) 

demo:

in [28]: whois([1,1,1,1, "216.58.208.228"],4) out[28]: 'net-216-58-192-0-1' 

or create dict , info in key/value pairings:

def whois(k,  i):     k = k[i]     = check_output(['whois',  k], universal_newlines=true)     print(who)     return  dict(map(str.strip,ele.split(":",1)) ele in re.findall('\w+:\s+.*', who)) d = whois([1,1,1,1, "216.58.208.228"],4)  print(d["nethandle"]) pprint import pprint pp pp(d) 

output:

net-216-58-192-0-1 {'address': '1600 amphitheatre parkway',  'cidr': '216.58.192.0/19',  'city': 'mountain view',  'country': 'us',  'nethandle': 'net-216-58-192-0-1',  'netname': 'google',  'netrange': '216.58.192.0 - 216.58.223.255',  'nettype': 'direct allocation',  'orgabuseemail': 'arin-contact@google.com',  'orgabusehandle': 'zg39-arin',  'orgabusename': 'google inc',  'orgabusephone': '+1-650-253-0000',  'orgabuseref': 'http://whois.arin.net/rest/poc/zg39-arin',  'orgid': 'gogl',  'orgname': 'google inc.',  'orgtechemail': 'arin-contact@google.com',  'orgtechhandle': 'zg39-arin',  'orgtechname': 'google inc',  'orgtechphone': '+1-650-253-0000',  'orgtechref': 'http://whois.arin.net/rest/poc/zg39-arin',  'organization': 'google inc. (gogl)',  'originas': 'as15169',  'parent': 'net216 (net-216-0-0-0-0)',  'postalcode': '94043',  'ref': 'http://whois.arin.net/rest/org/gogl',  'regdate': '2000-03-30',  'stateprov': 'ca',  'updated': '2013-08-07'} 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -