Principles of The IP Protocol
Principles of The IP Protocol
This process is known as a host file-based name resolution. You can send a
query to a DNS server and ask for the IP address of a specific host. If the name
has been registered properly, you will get a response from the server.
Here are some useful methods for gathering this kind of information:
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError:
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error: # not a valid address
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error: # not a valid address
return False
return True
print("IPV4 127.0.0.1 OK:"+ str(is_valid_ipv4_address("127.0.0.1")))
print("IPV4 127.0.0.0.1 NOT OK:"+ str(is_valid_ipv4_address("127.0.0.0.1")))
This is the execution of the previous script, where we can see that 127.0.0.1 is a
valid IPv4 address and ::1 is a valid IPv6 address:
IPV4 127.0.0.1 OK:True
IPV4 127.0.0.0.1 NOT OK:False
IPV6 ::1 OK:True
IPV6 127.0.0.0 NOT OK:False