linux命令行下查看ip归属地Python小脚本

<p style="margin: 0px 0px 15px; padding: 0px; border: 0px; font-size: 15px; vertical-align: baseline; word-wrap: break-word; color: #333333; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 冬青黑体, Helvetica, Arial, sans-serif; line-height: 22.5px;">今天以前追踪网络故障可能须要打开网址输入ip138等之类的网站查看问题ip归属地,这个脚本再mac/linux用来就是长期命令行操做的时候无需打开浏览器直接查询ip归属地来追踪网络归属地的小脚本,比起打开网页来直观更速度,借鉴了网上的一个网友的脚本,稍加改动,这种小小的细节可能没有多少技术含量,可是的确能起到节约一点时间的做用</p> <div class="cnblogs_code" style="font-family: 'Lucida Grande';"> <pre>ip归属地脚本,用来就是长期命令行操做的时候无需打开浏览器直接查询ip归属地来追踪网络故障的小脚本,比起打开网页来直观更速度html

<span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/env python </span><span style="color: #008000;"> </br>#</span><span style="color: #008000;">usage: python filename x.x.x.x</span>python

<span style="color: #0000ff;">import</span> urllib,re, sys <span style="color: #0000ff;">def</span> getip(ip=sys.argv[1]): url = <span style="color: #800000;">"</span><span style="color: #800000;">http://ip.qq.com/cgi-bin/searchip</span><span style="color: #800000;">"</span> data = <span style="color: #800000;">"</span><span style="color: #800000;">searchip1=</span><span style="color: #800000;">"</span>+ip html = urllib.urlopen(url, data).read().decode(<span style="color: #800000;">"</span><span style="color: #800000;">gb2312</span><span style="color: #800000;">"</span>) pat = re.compile(r<span style="color: #800000;">'</span><span style="color: #800000;"><span>(.*)</span></p></span><span style="color: #800000;">'</span>) result= re.findall(pat, html) <span style="color: #0000ff;">print</span> ip +<span style="color: #800000;">"</span><span style="color: #800000;">: </span><span style="color: #800000;">"</span> + result[0].encode(<span style="color: #800000;">"</span><span style="color: #800000;">utf-8</span><span style="color: #800000;">"</span>).replace(<span style="color: #800000;">'</span><span style="color: #800000;">&nbsp;</span><span style="color: #800000;">'</span>, <span style="color: #800000;">''</span>) getip()</pre>linux

</div>git

PS:最近对该脚本进行了改进,同时使用了ip.138,ip.cn,ip.qq查询地址,由于一方面有时候一个地址库查询未必准确的状况下,综合三者一块儿查询会增大命中率,脚本已经存放到http://git.oschina.net/mingmings/python_practice/blob/master/day02/ipwhere.py 中,欢迎指正浏览器

相关文章
相关标签/搜索