- Overview
- Transcript
8.1 Introducing the Socket
The fundamental building block of network programming in Python, and in many other languages, is the socket. The socket is what allows us to create connections between computers on a network. But before we can truly appreciate all that the socket can do for us, we need to start at the beginning. Let's start looking up the local computer's hostname and IP address.
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.1 Introducing the Socket
Now before we start getting into any sort of complicated networking mumbo jumbo and terminology, really what we wanna do is we wanna introduce kind of the basic building block of network programming in the Python language, and that's actually the socket. That's kind of one of the more lower level constructs that you can use to do network programming in order to issue requests and service requests and all sorts of things like that. But let's not get too far. Let's start with the very basics. So right now, we're gonna write a little utility that's going to do a couple of things for us. It is going to determine the host name of the machine that we're currently running on and it's going to determine our IP address. And so, that IP address is going to become very important as we begin to dig into the world of networking. And not necessarily just our own IP address, but IP addresses of other machines that we're trying to connect to. And I'm gonna show you how to do that as well also. So, let's start off with a couple of very simple things, like I said, by getting our host name and our IP address. So the first thing that we're gonna need to do is we wanna be able to use the socket in order to be able to do that. In order to use the socket, this is gonna become very consistent through all these lessons, is we're gonna need to import a socket. So that's gonna be that kind of building block that we're gonna be able to use consistently over and over throughout this course. At least until we get into some helper modules and libraries later on. So what we wanna do now is let's create a very simple function, and let's just call this get_info. And in here, we're going to use our socket. So we're going to use our socket in order to get our host name, or the host name of the machine that we're currently running on. So we'll just call this host_name and we're gonna set that equal to socket. And we're going to use the gethostname function on the socket. So it's gonna retrieve the host name of the system that we're running on. And then we're going to get the IP address of the system that we're running on by using another helper function here called socket.gethostbyname. And these two functions are going to be very helpful that you're gonna be able to use a lot over and over again to write little utilities for yourself to do things to figure out your IP address of your machine, and maybe do some configuration settings and those types of things. But in order for us to use the gethostbyname, we have to pass in the actual host name. And we're gonna pass in the host name that we retrieved from that first call. So now we have the host name and we have the IP address, let's just print them out to the screen. So we'll come down here and we'll use our print function. And we're gonna say, Host Name = and we'll just do a little bit of string formatting here. And we're going to say that it is host_name. So whatever came back is gonna be print out to the screen as Host Name, and we're gonna do print IP Address is gonna be equal to to the same thing. Here we do a little bit of string formatting, and this gonna be ip_address like that. So now we have a nice little function that's going to get our local machine info, the host name, and IP address. Now we just need to call our functions. So I'm simply going to do our little if_name is gonna be equal to main which basically just means that we can execute this from the command line, and we're gonna be doing this quite a bit throughout the course. You should probably consider maybe just copying this line of code so that you could reuse it over and over again, but it's quite simple. And then we're just gonna say get_info like this. So what we would assume is gonna happen at this point is when we run our application, we're gonna call get_info, we're gonna get the host name using the socket get_host name, and get the IP address by using socket get host by name using the host name that we retrieved, then we're gonna print it out to the screen. So let's go ahead and give that a shot. So we're gonna come over here and we're going to look in here. Now I have this one file in here so I'm gonna use Python, and remember this version of Python that I'm using right now is 2.7.9. So let's go ahead and do Python, and then we wanna execute our application here. And as you can see here my host name is equal to MacDev.local and my private IP address on my internal network right now is 10.0.0.84. So there you have it, that's a nice little quick utility that you can use to retrieve the host name and the IP address of your local system. Now we can tweak this a little bit and get the host name or get the IP address from a remote system, and I'm gonna show you how to do that in the next lesson.