Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

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()
    who = str(ou)
    print (who.find('NetName:'))
    who = re.split('NetName:',  who)[1]
    who = re.split('NetHandle',   who)[0]
    who = who.replace(r'
', '') 
    print (whois(k,  4))

Output:

108.160.172.204

520

None

Why python return "None" not "Dropbox"? If i take this code and:

k = '108.160.172.204'
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'
', '')
print (who)

Output is :

108.160.172.204

520

DROPBOX
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your function does not return a value so it returns None as all python functions that don't specify a 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()
    who = str(ou)
    print (who.find('NetName:'))
    who = re.split('NetName:',  who)[1]
    who = re.split('NetHandle',   who)[0]
    who = who.replace(r'
', '') 
    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
def whois(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'
', '')

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:

def whois(k,  i):
    k = k[i]
    who = check_output(['whois',  k], universal_newlines=True)
    return  dict(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:

def whois(k,  i):
    k = k[i]
    who = check_output(['whois',  k], universal_newlines=True)
    print(who)
    return  dict(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': '[email protected]',
 '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': '[email protected]',
 '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'}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...