- 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.Introduction2 lessons, 11:32
1.1Introduction02:03
1.2Prerequisites09:29
2.Python Building Blocks6 lessons, 1:08:07
2.1Introduction to the Interpreter09:51
2.2Numbers10:53
2.3Strings14:36
2.4Lists11:33
2.5Standard Input and Formatting12:00
2.6Building a Tip Calculator09:14
3.Controlling the Flow7 lessons, 1:20:10
3.1Conditional Statements12:47
3.2Looping With For09:36
3.3The Range Function10:41
3.4Looping With While13:06
3.5Creating Functions: Part 111:55
3.6Creating Functions: Part 208:49
3.7Building an Average Calculator13:16
4.Common Data Structures4 lessons, 46:49
4.1Lists, Stacks, and Queues, Oh My!11:45
4.2Dictionaries10:10
4.3Iterating Data Structures09:41
4.4Building a Sentence Analyzer15:13
5.Application Structure7 lessons, 1:15:12
5.1Modules09:08
5.2Packages11:24
5.3Classes09:53
5.4Attributes09:34
5.5Methods12:01
5.6A Special Calculator: Part 113:36
5.7A Special Calculator: Part 209:36
6.Collections7 lessons, 46:55
6.1What Are Comprehensions?06:32
6.2List Comprehensions06:08
6.3Dictionary Comprehensions06:38
6.4Map05:45
6.5Filter06:31
6.6Lambdas05:21
6.7Generators10:00
7.File I/O6 lessons, 48:51
7.1File Basics06:50
7.2Reading Entire Files07:49
7.3Navigating a File08:32
7.4Writing to Files07:22
7.5Reading and Writing to Files09:15
7.6Reading and Writing Complex Objects09:03
8.Networking5 lessons, 43:48
8.1Introducing the Socket04:39
8.2Getting a Remote IP Address06:42
8.3Handling Socket Errors07:58
8.4Create a Socket Server16:04
8.5Create a Socket Client08:25
9.Connecting to Network Services3 lessons, 34:27
9.1Getting the Current Time With NTP10:38
9.2Getting Websites With HTTP12:57
9.3Downloading Files With FTP10:52
10.Conclusion1 lesson, 02:08
10.1Goodbye02: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.