[英]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'\n', '')
print (whois(k, 4))
輸出:
108.160.172.204
520
None
為什么python返回“ None”而不是“ Dropbox”? 如果我采用此代碼並:
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'\n', '')
print (who)
輸出為:
108.160.172.204
520
DROPBOX
您的函數不會返回值,因此它會返回None,因為所有未指定返回值的python函數都會執行以下操作:
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'\n', '')
return who # return who
如果只需要輸出,也可以使用check_output
,也不需要重新分割:
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'\n', '')
還有多個python庫可以滿足您的需求,其中幾個是ipwhois , python-whois
如果只需要NetHandle
,則可以使用re查找:
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))
演示:
In [28]: whois([1,1,1,1, "216.58.208.228"],4)
Out[28]: 'NET-216-58-192-0-1'
或創建字典並獲取鍵/值配對中的所有信息:
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)
輸出:
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'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.