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.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.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.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.

Back to the top