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.3 Handling Socket Errors

Before we go too much further, we need to think about error handling. In this lesson, I'll show you how to detect when we have run into a network-related issue, and how to gracefully let the user know that there was a problem.

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.3 Handling Socket Errors

Before we start diving into some deeper and more complex socket programming concepts. What we want to do is take a step back for a moment and realize the magnitude of the situation that we're really dipping ourselves into. And that has to do with the fact that the second we start writing applications that reach out from our local machine and start going out on to the network and start talking to other machines. We have now infinitely increased the problems that could happen during the execution of our application. What am I trying to say here? Well, we're going to run into a lot of problems. So if you write an application that runs on your local machine, then odds are you have to deal with bugs that you've written. Or maybe faulty hardware. You have to catch exceptions. And do certain sorts of error handling. Well, the same things happens on a much bigger scale when you start reaching out over the network interface and start talking to other machines. Faulty hardware, timeouts, all sorts of other bad things can happen. So I just want to prepare you a little bit for some of the nastiness that can happen and show you some very simple things that you could do to gracefully handle it when problems like this happen. Now, also understand that it doesn't necessarily have to be faulty hardware or bad code, it could be bad input from the user. So let's see an example of this. So I have my nice little get_info enhanced script here, and what I wanna do is I wanna run this. So let's go ahead and say I want to run this again. That works pretty well, let's go ahead and say I want to check out the IP address for tutsplus.com, that works really well. But what happens if I type this in improperly, and I try to run this? I don't get a nice message that says, hey, I couldn't connect to that, there was a problem. Now, obviously, as an application, we can't code for all situations where maybe they typed in a valid host name, but maybe that system was down, or something else could have happened there. So we can't put nice messages for everything but we can stop this from showing up as a stack trace and getting all this nastiness here. Now maybe this line down on the bottom is reasonable. Maybe if we said the node name or the server name was provided or not known something along those lines, that's probably fine. And what this is also telling us is that this specific type of error that's happening is a GAI error. So that's a very specific networking error, so there was a problem in retrieving some information. Either about our local machine or a remote machine or something along those lines. So, maybe that information we do wanna present to the end user. So let's start to write a little bit of code to handle some of this. So what I wanna do first is I'm gonna grab all of this code that I had before and I wanna create a new file. And we'll paste this in and we'll save this, and we're just gonna call this 3 we'll call this, handlingerrors. Just like that. So that's the basic idea here. Now I'm gonna show you some tips and tricks of some basic structure here to handle these types of things and you once again can tweak this if you should so choose. Now we if we take a look back here, let's get a little bit specific about where the problem is happening. So, the problem is happening right here on this line, socket.gethostby name, by this host_name. And when we try to do that, there's a problem because this is not a valid host name, and everything blows up and it doesn't work. So let's be very specific about what we want to try to catch, or what we want to try to watch. As far as our application is concerned when it comes to the errors. So what we could do is we could wrap this entire function in some sort of error handling but I would say that's that bad practice. Where you're writing code to handle errors it's better to go in with a scalpel as opposed to a nuclear bomb. Because if you write one large kind of try, catch error handling system on your application or in your function. It's gonna be very difficult for you to maintain, because you're never gonna be really sure where those errors are happening. Now, there's a lot of techniques to be able to minimize those types of problems like writing short functions and single responsibility and all sorts of things like that. But I'm gonna save those types topics for another course. So what we wanna do here is we know that this is where the main problem is happening,right here. So let's be very specific. About where we want to introduce this air handling. So I'm gonna use a mechanism, the try except mechanism, and I want to try to run this entire block of code. Now I'm choosing to include the print lines here only because I know that those are probably gonna be working I shouldn't have to worry about those. And if this succeeds, then I want to run this as well, but if for some reason this doesn't, then I want to skip everything here and what do I want to do? I want to be very specific about what I am trying to catch as far as exception, so if we do get an exception, and as I said before this is a socket.gai error, so I want to look for that specifically. So we're gonna say socke.giaerror, and we'll refer to that as e. And what I'm gonna do now is I'm simply gonna print a message out to the user. I'm gonna say couldn't connect to hostName. And we'll put in some formatting here. So we'll put in the hostname we were trying to connect to. Then we'll also put what the error was here we'll say. Error and then we'll put another string here. And all I need to do now Is I need to provide those parameters, we have two of them. So we're gonna put them in open and close here, and we're going to attempt to connect to host name. And we're want to print out what the error was now, by using this e here, we're actually just gonna be getting this line right here which is kind of nice. So we're not gonna be getting a bunch of extra stuff in there. So let's go ahead and save that. So now if we come back in here and re run our application, that's not going to work obviously. Because I'm using the wrong file here. So this is gonna be this guy. So if I try to re run it now, we're gonna see. I couldn't connect to hostname www.tutsplus.com three and then the error was,we had node name no provided or not known. So that is a little bit better I feel like we are telling the user a little bit more about what's going on. Now you could make the argument that what if something else in here goes wrong maybe something else bad happens and it's not this specific error it's probably a good practice. To also put in here a much more general problem when it comes to the sockets because something else could be going on here. So we're going to be a little bit more generic as well to say maybe that wasn't the only error, maybe something else happened, so we can print something else here. And we'll just say this is a general connection error and since we don't really know anything else we're just going to print out that e and I did forget my quotation marks, like that. And we're just going to print out that error there. So we'll go ahead and save that. Now we've got a nice little application here that's going to retrieve information for us locally and remotely. And if something bad happens, then we're going to at least try to gracefully tell the end user that something happened and provide them with a little bit of information instead of a really ugly stack trace.

Back to the top