Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

manipulate ip address in python

import ipaddress
# initialize an IPv4 Address
ip = ipaddress.IPv4Address("192.168.1.1")

# print True if the IP address is global
print("Is global:", ip.is_global)

# print Ture if the IP address is Link-local
print("Is link-local:", ip.is_link_local)

# ip.is_reserved
# ip.is_multicast

# next ip address
print(ip + 1)

# previous ip address
print(ip - 1)

# initialize an IPv4 Network
network = ipaddress.IPv4Network("192.168.1.0/24")

# get the network mask
print("Network mask:", network.netmask)

# get the broadcast address
print("Broadcast address:", network.broadcast_address)

# print the number of IP addresses under this network
print("Number of hosts under", str(network), ":", network.num_addresses)

# iterate over all the hosts under this network
print("Hosts under", str(network), ":")
for host in network.hosts():
    print(host)

# iterate over the subnets of this network
print("Subnets:")
for subnet in network.subnets(prefixlen_diff=2):
    print(subnet)

# get the supernet of this network
print("Supernet:", network.supernet(prefixlen_diff=1))

# prefixlen_diff: An integer, the amount the prefix length of
        #   the network should be decreased by.  For example, given a
        #   /24 network and a prefixlen_diff of 3, a supernet with a
        #   /21 netmask is returned.

# tell if this network is under (or overlaps) 192.168.0.0/16
print("Overlaps 192.168.0.0/16:", network.overlaps(ipaddress.IPv4Network("192.168.0.0/16")))
Comment

PREVIOUS NEXT
Code Example
Python :: set http response content type django 
Python :: tqdm python 
Python :: create new dataframe with columns from another dataframe pandas 
Python :: remove duplicates function python 
Python :: python find in largest 3 numbers in an array 
Python :: minecraft python code 
Python :: affinity propagation python 
Python :: django media root 
Python :: python get file path from in os.walk 
Python :: pandas groupby aggregate 
Python :: change color of butto in thkinter 
Python :: python set grid thickness 
Python :: distplot with plotly 
Python :: exclude index column pandas 
Python :: change every value in a np array 
Python :: how to plotting bar on matplotlib 
Python :: how to delete a file in python 
Python :: python tkinter getting labels 
Python :: python password hashing 
Python :: How to recursively sort the elements of a stack, in Python? 
Python :: dynamic array python numpy 
Python :: initialize dictionary to zero in python 
Python :: python open and read file with 
Python :: split string and convert to int python 
Python :: how to remove numbers from a dataframe in python 
Python :: make tkinter label and input 
Python :: python remove punctuation 
Python :: make a nested list flat python 
Python :: number of words in a string python 
Python :: dataframe row print 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =