Checking IP Address Validity

Python 3: Automating Your Job Tasks Superhero Level: Automate Network Tasks with Python 3
12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.01
List Price:  €128.82
You save:  €36.80
£78.38
List Price:  £109.74
You save:  £31.35
CA$136.52
List Price:  CA$191.13
You save:  CA$54.61
A$150.48
List Price:  A$210.68
You save:  A$60.19
S$134.87
List Price:  S$188.83
You save:  S$53.95
HK$780.66
List Price:  HK$1,092.95
You save:  HK$312.29
CHF 91.34
List Price:  CHF 127.88
You save:  CHF 36.54
NOK kr1,052.31
List Price:  NOK kr1,473.28
You save:  NOK kr420.96
DKK kr686.58
List Price:  DKK kr961.24
You save:  DKK kr274.66
NZ$162.83
List Price:  NZ$227.97
You save:  NZ$65.13
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,719.82
List Price:  ৳16,408.23
You save:  ৳4,688.40
₹8,312.02
List Price:  ₹11,637.16
You save:  ₹3,325.14
RM469.75
List Price:  RM657.67
You save:  RM187.92
₦147,331.26
List Price:  ₦206,269.66
You save:  ₦58,938.40
₨27,796.36
List Price:  ₨38,916.02
You save:  ₨11,119.65
฿3,662.34
List Price:  ฿5,127.43
You save:  ฿1,465.08
₺3,217.98
List Price:  ₺4,505.30
You save:  ₺1,287.32
B$517.06
List Price:  B$723.91
You save:  B$206.84
R1,837.02
List Price:  R2,571.91
You save:  R734.88
Лв180.27
List Price:  Лв252.38
You save:  Лв72.11
₩136,066.92
List Price:  ₩190,499.14
You save:  ₩54,432.21
₪368.12
List Price:  ₪515.38
You save:  ₪147.26
₱5,814.46
List Price:  ₱8,140.48
You save:  ₱2,326.02
¥15,687.99
List Price:  ¥21,963.82
You save:  ¥6,275.82
MX$1,668.59
List Price:  MX$2,336.09
You save:  MX$667.50
QR364.34
List Price:  QR510.10
You save:  QR145.75
P1,357.56
List Price:  P1,900.64
You save:  P543.07
KSh13,298.67
List Price:  KSh18,618.67
You save:  KSh5,320
E£4,713.99
List Price:  E£6,599.78
You save:  E£1,885.78
ብር5,741.12
List Price:  ብር8,037.80
You save:  ብር2,296.67
Kz84,932.10
List Price:  Kz118,908.34
You save:  Kz33,976.24
CLP$90,081.08
List Price:  CLP$126,117.11
You save:  CLP$36,036.03
CN¥724.33
List Price:  CN¥1,014.10
You save:  CN¥289.76
RD$5,884.09
List Price:  RD$8,237.97
You save:  RD$2,353.87
DA13,459.27
List Price:  DA18,843.53
You save:  DA5,384.25
FJ$222.79
List Price:  FJ$311.92
You save:  FJ$89.12
Q776.15
List Price:  Q1,086.64
You save:  Q310.49
GY$20,904.26
List Price:  GY$29,266.81
You save:  GY$8,362.54
ISK kr13,793.62
List Price:  ISK kr19,311.62
You save:  ISK kr5,518
DH996.63
List Price:  DH1,395.33
You save:  DH398.69
L1,771.78
List Price:  L2,480.56
You save:  L708.78
ден5,664.24
List Price:  ден7,930.17
You save:  ден2,265.92
MOP$804.05
List Price:  MOP$1,125.70
You save:  MOP$321.65
N$1,836.30
List Price:  N$2,570.90
You save:  N$734.59
C$3,677.56
List Price:  C$5,148.73
You save:  C$1,471.17
रु13,287.88
List Price:  रु18,603.56
You save:  रु5,315.68
S/373.53
List Price:  S/522.95
You save:  S/149.42
K388.30
List Price:  K543.64
You save:  K155.33
SAR375.02
List Price:  SAR525.05
You save:  SAR150.02
ZK2,666.83
List Price:  ZK3,733.66
You save:  ZK1,066.83
L457.94
List Price:  L641.14
You save:  L183.19
Kč2,276.37
List Price:  Kč3,187.01
You save:  Kč910.64
Ft35,391.42
List Price:  Ft49,549.41
You save:  Ft14,157.98
SEK kr1,062.40
List Price:  SEK kr1,487.41
You save:  SEK kr425
ARS$89,066.09
List Price:  ARS$124,696.09
You save:  ARS$35,630
Bs690.41
List Price:  Bs966.60
You save:  Bs276.19
COP$385,967.67
List Price:  COP$540,370.17
You save:  COP$154,402.50
₡51,227.77
List Price:  ₡71,720.93
You save:  ₡20,493.15
L2,469.21
List Price:  L3,457
You save:  L987.78
₲751,546.38
List Price:  ₲1,052,195.01
You save:  ₲300,648.62
$U3,849.38
List Price:  $U5,389.29
You save:  $U1,539.90
zł391.29
List Price:  zł547.82
You save:  zł156.53
Already have an account? Log In

Transcript

Now it's time to determine if each of the IP addresses in the first text file is valid or not, meaning it follows the correct format and is not a reserved IP address. There are many reserved IP addresses. And you can find all of them right here on Wikipedia by following this link which is also attached to this lecture by the way, however, we will handle some of the most common ones in our code, meaning we will make sure that the IP addresses in our file are not loopback multicast broadcast link local or addresses reserved for future use, meaning they don't start with 127 2242239 all to five fives 169 dot 254 or 242 to five five. For this we should define the conditions for an IP to be valid. Of course using an if statement. This statement will give valuate multiple conditions simultaneously, because we want our IP addresses to meet all of these conditions at the same time.

Otherwise, if at least one of the conditions is evaluated as false, the entire if statement becomes false, because we are using the end logical operator between the conditions. And in this case, the L statement kicks in and prints a message to the screen and then quit the program. Also, before moving on to analyze the conditions, notice that we used a function called ip addr valid, which will later be called in the main application from within this module. Inside this function, we are using a for loop to iterate over the list of IP addresses that we got after reading the file and creating a list in the previous lecture. This is the for loop right here. This list is taken by the function as a parameter, as you can see right here in between its parentheses and you will see this in action.

When all of these modules, we will imported and used inside the main application. So for each IP address in the file for IP enlist, we are performing the same tests regarding the validity of the IP address. Now, let's consider that the for loop extracts the first IP address, since each addressing the file was placed on a separate line, when reading the file and creating the list using the read lines method, the IP addresses will each be followed by a backslash n a newline character. So using the our strip method right here, which is a string specific method that strips a character from the right side of a string, we are getting rid of the backslash n and keeping just the IP address itself. Actually, let me prove this to you. I will open up the Python interpreter and then open the IP dot txt file.

Okay, so let's open the file using the open function. This is the path to my file and we are going to open it for reading. Now using the read lines method. Let's create the list of IP addresses. So a dot read lines returns this list right here. Notice that the first two IP addresses in the list have that annoying backslash n at the end.

Now let's strip that away using a similar for loop. So for i in a dot read lines, colon, i dot, our strip of backslash n. Notice we didn't get any result Why? Because we already went through the file. So always remember to use the seek method. So a dot seek of zero. And now let's run our for loop again.

Great, we got rid of the backslash ends. So that's exactly what we're doing inside our module as well. So I hope it's all clear now. Now let's get back to the code. And notice that we're also splitting each IP address in the list using the.as, a delimiter, and then saving the list generated by the split method using the octet list variable. Why?

Because we want to Extract each of the four octets of each IP address and analyze them by checking the conditions in the if statement below. So going back to the Python interpreter, let's take the first IP address in the list and extract its octets in the form of a list using the split method, first of all, a dot seek of zero. And now let's just use a dot read lines of zero. Because we're interested in the first element in the list, dot, our strip of backslash n, we're stripping the backslash. And once again, that split and we're using the dots as a delimiter. Sorry, my mistake, I typed in red line instead of red lines.

So let me try this again. A dot seek of zero and a dot red lines instead of red line. Okay, first of all, notice a very cool thing here. We chained and applied to methods on a string at the same time. So we have the list general By red lines right here, then we only care about the first element in that list. So we use the index zero, then we write strip the newline character right here.

And finally, we split that IP address into four octets. Using the.as, a delimiter. The result is those octets being the elements of the list. The same is done for each IP address in the file, then it goes through the conditions specified inside the if statement. And now it's time to see what should those conditions be? Well, let's think of a valid non reserved IP address.

First, it should have four octets. Right, this is obvious. So our list of octet stored in the variable octet list should have four elements. This means Len of octet list equals equals four. This is the first condition. Now let's focus on the value of the first octet.

First, notice that each element of octet list of this list, as you can see right here is a string. That's why inside our if statement, we have to convert it to an integer using the INT function for comparing it to other values. Otherwise, we wouldn't be able to compare it with any integers. Now we know that an IP address starting with two to four, or higher octet is a multicast or a broadcast address, or one which is reserved for future use. That would be Class D or Class C network for all your gigs out there. Since we're interested in unicast, addresses only, we're going to specify in the same if statement, that octet list of zero so the first octet in our list should be between one and two to three inclusively.

And this is the condition right here, octet list, less than or equal to two, three, and at the same time, greater than or equal to one We also know that in the unicast range, we have two special cases. First, the addresses starting with 127 are reserved for loopback interfaces, so we must exclude them as well. That's why the third condition is that octet list of zero is different than 127. The second special case is any address starting with 169 dot 254. These are auto assigned by Windows machines to interfaces configured for DHCP that are unable to contact a DHCP server. So we should add this condition as well.

This is the condition right here. Now as a side note, notice that I used a pair of parentheses for each condition. I did this for better readability. We have multiple conditions inside this if statement. So we must be able to read them as easy as possible. Also, I remind you that we have a logical AND operator between conditions.

This means that the IP address entered by the user should meet all of these conditions at the same time in order to be considered valid, because remember, when we talked about logical operators, I said that when using end, all operations should be true in order for the expression to be evaluated as true as a whole. So this if statement right here says, if the octet in your IP address meet all of these conditions right here, then it is a valid IP address, and the program should continue evaluating the next iteration, the next IP address, otherwise, a message is printed out and the execution stops, as you can see under the else clause. Now back to our 169 to five for condition, and this is it right here. Notice that this condition is made of two other smaller conditions, meaning the first octet should not be 169.

And the second octet should not be 254. But Why did I use the OR operator in between them? At first sight, you would be tempted to say that there should be an end logical operator, they're not an OR operator. If I would have used and between them, then I would have excluded some valid addresses to like, for example, 169 dot 200 dot one dot one, or 10 dot 254 dot one dot one, because the end operator would have told Python that both operands should be true at the same time. So 169 and 254 would have been totally forbidden inside an IP address, which is not the case because we only care about the unique combination of 169 dot 254. Let me prove this to you.

And then we will move on to the next module. So opening up the Python interpreter, let's consider a to be this IP address. And let's see we have octet list equals a dot split using the.as a delimiter. Now, let's see At least Okay, now let's use the same conditions and see how the if else block behaves both when using the end operator. And when using the old logical operator between our two smaller conditions. So let me paste in a similar code.

So inside the if statement, we have the exact same conditions. And for the condition that we're discussing right now, this one right here, we still have the or logical operator between the two smaller conditions. So if the IP address is valid, then we print valid to the screen and we print invalid. So notice that in this case, our IP address that we just defined is a valid IP address, since we have 168 dot 254, not 169 dot 254. So this is not a link local address. This means that our code right here should return valid right?

And it did it does. Now let's change the code The if statement. Replace or with and between the two smaller conditions. Okay, so instead of all we type in and and now using the same IP address, let's run the code again. And this time we have invalid. So in this case, notice that using the end operator will result in a valid IP address being evaluated as invalid.

Since now, 169 and 254 are both individually completely forbidden in the first and second octet, respectively. And of course, that's not what we're looking to achieve. We want to cover only the case when the combination of the two, namely 169 254, is in the first two octets of the IP address. Now, if we change our IP address to a link local one using 169, and 254, and then use the OR operator instead of end, let's see what happens. Their behavior should be the normal one. So the results should be invalid, right?

Because this time, we truly have a reserved IP address. So let's redefine a, and enter an invalid or reserved IP address 169 dot 254 dot two dot two. And now using the OR operator as we do within our code, let's run this if else block again. And we still got valid because I forgot to also split the address. So now let's see octet list, the new octet list list. Okay, now if we run the code once again, using the OR operator in between the two sub conditions, we are getting indeed invalid, because now we have a link local IP address, which cannot be considered valid for the purpose of our application.

Okay, so great. This proves my point. I'll see you in the next lecture to discuss yet another module for our application.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.