Skip to content

Commit fc7222b

Browse files
committed
check_ip138.py
1 parent b81b6fa commit fc7222b

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@
2727
2. check_ping.py 多进程检测ping,并取值
2828

2929
默认开启4个进程,需要将hosts.txt IP列表文件放入同一目录下,IP列表每行一个,支持域名、IP
30+
3. check_ip138.py 通过ip138检测IP(域名)归属地
31+
32+
使用方法: python check_ip138.py 192.168.1.1

check_ip138.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
import urllib
4+
import re
5+
import sys
6+
7+
8+
9+
def ISIP(s):
10+
return len([i for i in s.split('.') if (0<= int(i)<= 255)])== 4
11+
12+
def URL(ip):
13+
uip=urllib.urlopen('http://wap.ip138.com/ip.asp?ip=%s'%ip)
14+
fip=uip.read()
15+
rip=re.compile(r"<br/><b>查询结果:(.*)</b><br/>")
16+
result=rip.findall(fip)
17+
print "%s\t %s" %(ip,result[0])
18+
19+
def DO(domain):
20+
url=urllib.urlopen('http://wap.ip138.com/ip.asp?ip=%s'%domain)
21+
f=url.read()
22+
r=re.compile(r'&gt; (.*)<br/><b>查询结果:(.*)</b><br/>')
23+
result=r.findall(f)
24+
#print type(result)
25+
for i in result:
26+
print "%s\t %s\t %s\t" %(domain,i[0],i[1])
27+
28+
if __name__ == "__main__":
29+
if len(sys.argv) < 2:
30+
print "请输入IP地址或者域名 (例如:192.168.1.1 / www.baidu.com)"
31+
sys.exit()
32+
INPUT=sys.argv[1]
33+
if not re.findall('(\d{1,3}\.){3}\d{1,3}',INPUT):
34+
if re.findall(r'(\w+\.)?(\w+)(\.\D+){1,2}',INPUT) :
35+
DOMAIN=INPUT
36+
DO(DOMAIN)
37+
else:
38+
print "输入的IP地址和域名格式不对!"
39+
else:
40+
if ISIP(INPUT) :
41+
IPADDRESS=INPUT
42+
URL(IPADDRESS)
43+
else:
44+
print "IP 地址不合法,请重新输入!"
45+

0 commit comments

Comments
 (0)