我在网址列表上运行与ping的快速rtt比较.我想提取ip所以我可以运行whois并获取地理位置.当我在google.com和facebook.com专门开始的网址列表上运行此操作时会发生的情况是,dip匹配谷歌的正则表达式,但不是facebook,即使ping的输出都是相同的格式.下面的代码可以更好地了解发生了什么.
urls = ["google.com", "facebook.com"]
ip_regex = re.compile('[1-9]+\\.[1-9]+\\.[1-9]+\\.[1-9]+')
time_regex = re.compile(' [\.1-9]+/.*/.*/.* ms')
for url in urls:
output = ""
print url
ping = subprocess.Popen(["ping", "-c", "3", url], stdout=subprocess.PIPE)
while ping.poll() == None:
output += ping.stdout.read()
output += ping.stdout.read()
#DEBUG
print "OUTPUT"
print output
ip = ip_regex.findall(output)
print ip
ip = ip[0]
times = time_regex.findall(output)
print times
os.system('whois ' + ip + ' | egrep "Country|StateProv|City"')
Run Code Online (Sandbox Code Playgroud)
对于其他人,ip_regex在facebook(#2)上是否失败?为什么?
facebook的IP中有零.1-9不包括零.
当我们在它时,使用原始字符串表示正则表达式:
r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
Run Code Online (Sandbox Code Playgroud)
使用\d该类匹配数字:
r'\d+\.\d+\.\d+\.\d+'
Run Code Online (Sandbox Code Playgroud)
别忘了修复其他正则表达式:
r' [\.0-9]+/.*/.*/.* ms'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
136 次 |
| 最近记录: |