0% found this document useful (0 votes)
52 views4 pages

Principles of The IP Protocol

This document discusses how to resolve and validate IP addresses using the socket package in Python. It explains that the socket.gethostbyname() function can be used to resolve a hostname into an IP address by querying a DNS server. It also presents code that uses socket.inet_pton() and socket.inet_aton() to validate whether a string represents a valid IPv4 or IPv6 address format. The code sample demonstrates validating some example addresses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views4 pages

Principles of The IP Protocol

This document discusses how to resolve and validate IP addresses using the socket package in Python. It explains that the socket.gethostbyname() function can be used to resolve a hostname into an IP address by querying a DNS server. It also presents code that uses socket.inet_pton() and socket.inet_aton() to validate whether a string represents a valid IPv4 or IPv6 address format. The code sample demonstrates validating some example addresses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Intermediate

Principles of the IP protocol


In this section, you will learn how to resolve and validate an IP address with the
socket package.
Resolving the IP address with the
socket package
If you would like to see the local machine IP, you can do so using the ifconfig
command in Linux and the ipconfig command in Windows. Here, we'll do this in
Python using the built-in function:
>>> import socket
>>> socket.gethostbyname('python.org')
'10.0.2.15'

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:

: This allows us to obtain a domain name from


socket.gethostbyaddr(address)
the IP address.
: This method converts a hostname into IPv4
socket.gethostbyname(hostname)
address format. The IPv4 address is returned in the form of a string. This
method is equivalent to the nslookup command that we can find in many
operating systems.
Validating the IP address with the
socket package
We can use also the socket package to validate an IP address in both IPv4 and
IPv6.

You can find the following code in the check_ip_address.py file:


import socket

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")))

print("IPV6 ::1 OK:"+ str(is_valid_ipv6_address("::1")))


print("IPV6 127.0.0.0 NOT OK:"+ str(is_valid_ipv6_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

You might also like