if "224.0.0.0" <= "200.110.11.11" <= "239.255.255.255":
print("Multicast")
else:
print("Unicast")
This above code seems to work fine in finding the ip:200.110.11.11 is multicast or not, how does string comparison works ? Can this be used as a valid code in determining the ip address as multicast or not ?
In python, strings are compared lexicographically based off how early each character places in an ascii table. This means python goes through the first character of each string and compares the ascii values of those characters, then the second character of each string, and so on, until an operator is satisfied or the end of a string is reached.
This would not work for all cases of your code because not every ip will have the periods in the same indices of the string. To solve this you can use the built-in ipaddress
module in python 3.x like this:
from ipaddress import IPv4Address
low = IPv4Address('224.0.0.0')
high = IPv4Address('239.255.255.255')
test = IPv4Address('200.110.11.11')
if low <= test <= high:
print('Multicast')
else:
print('Unicast')