Skip to content

Instantly share code, notes, and snippets.

@marhar
Created August 15, 2015 06:58
Show Gist options
  • Save marhar/eea1a4d5c43da81b5a26 to your computer and use it in GitHub Desktop.
Save marhar/eea1a4d5c43da81b5a26 to your computer and use it in GitHub Desktop.
Python: is this address a multicast address?
def ismulticast(hostname):
"""is this address a multicast address?"""
# -- yes if bits 0-3 are 1110
import socket
import struct
ip=socket.gethostbyname(hostname)
mreq=struct.pack("4sl",socket.inet_aton(ip),socket.INADDR_ANY)
return ord(mreq[0]) & 0xf0 == 0xe0
for i in ('localhost','138.72.131.168','239.239.239.7','10.1.1.1'):
print ismulticast(i),i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment