Skip to content Skip to sidebar Skip to footer

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()

Solution 1:

Your function does not return a value so it returns None as all python functions that don't specify a return value do:

defwhois(k,  i):
    k = str(k[i])
    print (k)
    whois = subprocess.Popen(['whois',  k], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    ou, err = whois.communicate()
    who = str(ou)
    print (who.find('NetName:'))
    who = re.split('NetName:',  who)[1]
    who = re.split('NetHandle',   who)[0]
    who = who.replace(r'\n', '') 
    return who # return who

You can also use check_output if you just want the output, you also don't need re to split:

from subprocess import check_output
defwhois(k,  i):
    k = str(k[i])
    who = check_output(['whois',  k], universal_newlines=True)
    who = who.split('NetName:')[1]
    who = who.split('NetHandle')[0]
    return  who.replace(r'\n', '')

There are also multiple python libraries that will do what you want a couple of which are ipwhois, python-whois

If you just want the NetHandle you can use re to find that:

defwhois(k,  i):
    k = k[i]
    who = check_output(['whois',  k], universal_newlines=True)
    returndict(map(str.strip,ele.split(":",1)) for 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 a dict and get all the info in key/value pairings:

defwhois(k,  i):
    k = k[i]
    who = check_output(['whois',  k], universal_newlines=True)
    print(who)
    returndict(map(str.strip,ele.split(":",1)) for ele in re.findall('\w+:\s+.*', who))
d = whois([1,1,1,1, "216.58.208.228"],4)

print(d["NetHandle"])
from pprint import pprint as 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'}

Post a Comment for "Re.split Return None In Function But Normaly Works Ok"