[Python] IP地址信息工具(IPAIT)
今天我要向大家展示另一个我在空闲时间完成的趣味项目——IP地址信息工具(IPAIT)。这个工具使用Python语言创建,我们可以使用它来查找任何IPv4或IPv6公网IP地址的信息。
这是一个简单的工具,所以我不打算深入讨论其内部原理。它支持Python3,并且需要互联网连接才能运行。该工具可以提取以下信息:
此外,您还可以通过向ipait.py添加适当的行来获取以下详细信息(您可以在第84行之后添加额外的行):
- 主机名 -
print ("Hostname: " + js["hostname"]);
- 城市 -
print ("City: " + js["city"]);
- 区域 -
print ("Region: " + js["region"]);
- 邮政编码 -
print ("Postal: " + js["postal"]);
以下是不含额外行的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/usr/bin/env python
#IP地址信息工具(IPAIT)
#作者: Chandika Lasiru
#博客: https://clasiru.blogspot.com
import socket;
import json;
from urllib.request import urlopen;
banner = """
██╗██████╗ █████╗ ██╗████████╗
██║██╔══██╗██╔══██╗██║╚══██╔══╝
██║██████╔╝███████║██║ ██║
██║██╔═══╝ ██╔══██║██║ ██║
██║██║ ██║ ██║██║ ██║
╚═╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝
IP地址信息工具
by Area Master
使用此工具前:-
01. 您必须拥有互联网连接。
02. 您必须输入有效的公网IP地址。
"""
print (banner, "\n");
def google_ok():
try:
urlopen('https://www.google.com', timeout=10);
return True
except:
return False
return True
def yahoo_ok():
try:
urlopen('https://www.yahoo.com', timeout=10);
return True
except:
return False
return True
def site_ok():
try:
urlopen('https://ipinfo.io', timeout=10);
return True
except:
return False
return True
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address);
except socket.error:
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address);
except socket.error:
return False
return True
host = input(" 输入IPv4或IPv6地址: ");
if google_ok() or yahoo_ok():
if site_ok():
if is_valid_ipv4_address(host) or is_valid_ipv6_address(host):
whois = "https://ipinfo.io/" + host + "/json";
data = urlopen(whois).read();
js = json.loads(data)
if "bogon" in js :
print ();
print (" 错误: 您的IP地址不是公网IP地址!");
else:
print ();
print (" IP地址: " + js["ip"]);
print (" 国家: " + js["country"]);
print (" 位置: " + js["loc"]);
print (" ASN/组织: " + js["org"]);
print (" 时区: " + js["timezone"]);
#您可以在此处添加额外的行。
else:
print ();
print (" 错误: 您的IP地址不是有效的IP地址!");
else:
print ();
print (" 错误: 无法检索信息!");
else:
print ();
print (" 错误: 检查您的互联网连接!");
|
我希望您喜欢我的编码。此外,这个工具可以自由修改;您可以根据自己的喜好进行任何更改。我为此创建了GitHub存储库。您可以在这里查看:https://github.com/clasiru/IP_Address_Information_Tool