Skip to content

Commit 9269dd4

Browse files
author
tuntun
committed
查询ip、网址归属地(通过ip168)
1 parent dcae846 commit 9269dd4

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

check_ip.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
4+
import urllib.request
5+
import re
6+
import sys
7+
8+
def ISIP(s):
9+
return len([i for i in s.split('.') if (0<= int(i)<= 255)])== 4
10+
11+
def URL(ip):
12+
uip = urllib.request.urlopen('http://wap.ip138.com/ip.asp?ip='+ip)
13+
fip = uip.read()
14+
rip = re.compile("<br/><b>查询结果:(.*)</b><br/>")
15+
result = rip.findall(fip)
16+
print("%s\t %s" % (ip, result[0]))
17+
18+
19+
def DO(domain):
20+
url = urllib.request.urlopen('http://wap.ip138.com/ip.asp?ip='+domain)
21+
f = url.read().decode('utf-8')
22+
r = re.compile('<br/><b>查询结果:(.*)</b><br/>')
23+
result = r.findall(f)
24+
#print type(result)
25+
for i in result:
26+
print("%s %s %s" % (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('(\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 地址不合法,请重新输入!")

0 commit comments

Comments
 (0)