Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

8.2 Getting a Remote IP Address

So far, we've only used sockets to get information about our local machine. And sockets are meant to reach out onto the network, right? So let's use sockets to get the IP address of another machine on the internet.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


8.2 Getting a Remote IP Address

So we did a pretty good job in the last lesson of creating ourselves a little utility that's gonna give us some information, namely the host name and IP address of the system that we're currently running on. And, we did that using our newest friend the Socket. But one thing that we're gonna start running into very quickly, once we start talking about the world of networking, is what exactly is networking? Well, networking is the communication of two or more systems on a network. This is very helpful if I want to know about myself. But, what if I wanna know about where another machine is by retrieving it's IP address, so I can know where I need to send that information to, or where I need to make a connection to. And that's what we wanna do, we wanna take what we've done so far we want to adapt it a little bit. So, that it's a little bit more usable to find not only local information, but also remote information as well. So, let's go ahead and create a new file here, so I'm gonna save this and I'm going to call this 2- we're gonna call this getting info how about enhanced? So, we're going to give this a little bit more power over what we had before, but I'm going to start with the same basic structure. All right, so what are we wanna be able to do now? So, how about if we were to introduce the concept of being able to pass in parameters or to pass at an argument to our script. That will say the domain name or the host name of a remote machine that was accessible via DNS. So let say, I wanted to figure out what was the IP address of www.google.com or Touchplus.com or some other machine that is identifiable by name on a network somewhere. So, that means it's registered a DNS somewhere. So, what I wanna do now is adapt this just a little bit, so the first thing I wanna do is I wanna be able to accept input from the command line, and in order to do that we gonna need to import SYS. So, that's going to give me access to a list called argv of all of the parameters that are being passed in. To this application from the command line. So, let's go ahead and start there, so the first thing that I wanna do is to get access to that. And now, the way we do that like I said is using sys.argv and this is a list that I can get at all the parameters. Now, what got you here if you're not used to using this construct is that, all of the things that show up on the command line will show up in this list, including the name of our file. So, two dash getting info enhanced dot p y is going to show up in this list so just kind of keep that in mind. That's gonna be the first argument at the zero index, so anything beyond that from one and beyond is what we're going to get. So, what I wanna do now is I want to check to see if there are no additional arguments. And if there's no additional arguments, I just wanna run my normal applications. So let's try that first. So I'm gonna say if, sys.argv but more than that, I wanna check the length of that. So I'm gonna say, if the length of sys.argv == 1, meaning it only has the name of my script, and not any additional host names that I want to check for. Then let's just go ahead and leave it, and we're going to simply run our application just like this. So, let's go ahead and save those changes. So, let's come back over here. And now, I wanna run Python and I want to run my new script, and everything should continue to work the way that it did before, I could see Mac dev dot local and my private IP address. So I can see that now, but I want to be able to pass some information in here. So, what I'd like to do is I'd like to say else, meaning there was something else in there, and I want to pass that to get info. So I want to be able to say sys.argv. Assuming that they're only passing in one that obviously you can make some enhancements here and pass in. Some additional information, as well as be able to check for proper usage of this script and print on a used statement and all those types of cool things. I'm gonna leave that as a exercise for you, cuz right know all that I gonna be able to do is, use my little function here to get my able to get that information, into that point. I'm going to assume, that the information is being provided correctly. So now, I'm going to assume that I need pass something in to my function here. So I'm gonna say, I want to pass in this host_name like this. But, I want to default it to an empty string. To say, you don't have to pass anything to this from the outside world. And if you don't, it's gonna be an empty string. But if you do pass something in, it's going to be whatever you've passed in. So, we have to do a little bit of checking here. So, we're gonna say if my host name is equal to an empty string. Then I need to do this line right here, so I need to actually get my local host name. And then after that, we'll get the IP address of either what was passed in or my local machine name and we'll just print things out here. So, let's go ahead and save that. Now, we can come back and then let me rerun this and I should get the same result, and I do, which is good. But now, I wanna be able to pass something in. So, let's see if our code is going to work properly. Let's say I wanted to see where is, www.google.com. And I'm gonna see that my host name, Google.com, is gonna be at 172.217.4.2.28. Now, do understand that for a lot of these machines that are out in the Internet, they're gonna have multiple IP addresses. Because, they are large websites, large domains. They probably are behind some sort of switching infrastructure. There's probably multiple entry points into those, because they receive a lot of traffic. So I can probably run this multiple times and it's possible that every once in a while I could get different IP addresses. So just understand that, just because you get the same one one time, doesn't mean that they only have one IP address. It could be multiple IP addresses coming from different machines, depending on whatever sort of machine you are hitting at that particular time. So, not too bad. What we've done so far is we've been able to, provide a nice utility that you can use to now not only get the information about your local machine. But also, find the address of a remote machine somewhere else that you now have access to.

Back to the top