Use Python to get and process IP's type and formatting methods
- 2021-01-19 22:17:59
- OfStack
The decision between a public network and a private network is a simple one, as long as you remember the three segments of a private network. However, for people with short memories or machines with poor learning, a method of judgment may be necessary.
Write the following script:
from IPy import IP
ip1 = IP('192.168.1.2')
ip2 = IP('11.12.13.14')
print("ip1 type: %s" % ip1.iptype())
print("ip2 type: %s" % ip2.iptype())
print("ip2 int value: %d" % ip2.int())
print("ip2 hex value: %s" % ip2.strHex())
print("ip2 bin value: %s" % ip2.strBin())
print("IP for 0x1234567: %s" % IP(0x1234567))
The running results are as follows:
ip1 type: PRIVATE
ip2 type: PUBLIC
ip2 int value: 185339150
ip2 hex value: 0xb0c0d0e
ip2 bin value: 00001011000011000000110100001110
IP for 0x1234567: 1.35.69.103
As can be seen from the above results:
1, ES11en1 private address;
2, ip2 is the public network address;
3. Different types of IP can be converted freely;