[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 Address Information Tool (IPAIT)
#Author: Chandika Lasiru
#Blog: https://clasiru.blogspot.com
import socket;
import json;
from urllib.request import urlopen;
banner = """
██╗██████╗ █████╗ ██╗████████╗
██║██╔══██╗██╔══██╗██║╚══██╔══╝
██║██████╔╝███████║██║ ██║
██║██╔═══╝ ██╔══██║██║ ██║
██║██║ ██║ ██║██║ ██║
╚═╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝
IP Address Information Tool
by Area Master
Before using this Tool:-
01. You must have Internet Connection.
02. You must have to enter valid Public IP Address.
"""
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(" Enter IPv4 or IPv6 Address: ");
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 (" Error: Your IP Address is not a Public IP Address !");
else:
print ();
print (" IP Address: " + js["ip"]);
print (" Country: " + js["country"]);
print (" Location: " + js["loc"]);
print (" ASN/Organization: " + js["org"]);
print (" Time Zone: " + js["timezone"]);
#You can add additional lines here.
else:
print ();
print (" Error: Your IP Address is not a Valid IP Address !");
else:
print ();
print (" Error: Can't retrive Information !");
else:
print ();
print (" Error: Check your Internet Connection !");
|
希望您喜欢我的编码。这个工具可以自由修改,您可以进行任何您喜欢的更改。我为这个项目创建了GitHub仓库,您可以在这里查看:https://github.com/clasiru/IP_Address_Information_Tool