Using the IPAddress Library (with Lab)
Using the example by importing the ‘ipaddress’ module from Python which allows you to check whether a list of router IPs are valid or not. To simplify the code as the firs iteration I have created a list then a for loop to check the list of IP addresses.The example code is below and I will explain line by line before moving to using Functions.
The first line of code is to import the ipaddress library module, it is designed to handle IP address related tasks. In this instance, we are using the library to check if a list of IPs are valid or not.
From line 3 – 11 we create a list, and the list includes IP addresses and words.
Line 13, I create a for loop called ‘index’ this could be calle anything, I then create a index numbering by calling the ‘enumerate()’ function and called it ‘list’. In the enumberate function I call in the list I created in line 3 called ‘ip’.
Line 14 begins witht he ‘try’ which tests a block of code to see if there are any errors.
Line 15 I create a variable called ‘ip_obj’ and that then calls in the module ‘ipaddress’ from line 1 and then the ‘IPv4Address’ object and calls in the ‘list’ which is tied to the enumerate list of ips. The object from the IP address.IPv4Address’ purpose is to check the list whether they are valid IP addresses or not. If so, then line 16 prints to state it is a valid IP.
Line 17 is how to handle the try code, so if the code has no errors then use the keyword ‘ValueError’ (keyword in the ipaddress library module) which means it is not a valid IP, if so then to print it is not a valid IP in line 18.
Another example is to create a function which means you can re-use the code to save time.
Using the example by importing the ‘ipaddress’ module from Python which allows you to check whether a list of router IPs are valid or not. To simplify the code as the firs iteration I have created a list then a for loop to check the list of IP addresses.The example code is below and I will explain line by line before moving to using Functions.
Line 3, I create a function called ‘check_ip()’.
The same for loop is then used in the function.
Line 4, I create a for loop called ‘index’ this could be calle anything, I then create a index numbering by calling the ‘enumerate()’ function and called it ‘list’. In the enumberate function I call in the list I created in line 11 called ‘ip’.
Line 5 begins with the ‘try’ which tests a block of code to see if there are any errors.
Line 6 I create a variable called ‘ip_obj’ and that then calls in the module ‘ipaddress’ from line 1 and then the ‘IPv4Address’ object and calls in the ‘list’ which is tied to the enumerate list of ips. The object from the IP address.IPv4Address’ purpose is to check the list whether they are valid IP addresses or not. If so, then line 7 prints to state it is a valid IP.
Line 8 is how to handle the try code, so if the code has no errors then use the keyword ‘ValueError’ (keyword in the ipaddress library module) which means it is not a valid IP, if so then to print it is not a valid IP in line 9.
From line 11-19 we create a list, and the list includes IP addresses and words.
Finally, we call the function so it begins to test the code out.