Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Checking the Socket Status

00:00 Here are a couple of tools you can use to check the status of the sockets your server programs are listening to.

00:07 Most modern operating systems have several tools to check the state of sockets. One is netstat. As the name suggests, it shows the network status as seen by your computer.

00:18 Useful options are -a, which causes netstat to list just active ports, and -n, which means to show the numeric addresses and not domain names.

00:29 There will be a lot of output for this, but the lines you want to look for are those with the port numbers your server is listening to as it’s running. The columns of interest include the first one, tcp4, which indicates it’s a TCP socket connection using version four addressing, which you remember from the initializer in the server program.

00:51 Then you can see a column with the HOST.PORT that the socket is using, and the last column shows the socket is in the listening state.

01:00 If you used the empty string for the host to indicate accepting connections from any computer on the network, then the local address will be an asterisk (*) in the HOST.PORT column.

01:14 macOS and Linux also provide another tool called lsof, which stands for list open files. At some level of abstraction, sockets look similar to files where you read or save data instead of receiving or sending data.

01:30 So you want to use the -i option to show only open Internet sockets. -n again will provide information using numeric addresses.

01:40 When you run it, you’ll see a lot of the same information as netstat, but it’ll also show you the type of process and username that socket is being used by.

01:51 If your copy of Linux doesn’t have lsof, it shouldn’t be too hard to acquire. Both of these programs have additional command-line options. Be sure to check the documentation for other information the programs can give you. So, when should you use them?

02:09 Here are a couple of errors you might see that suggest you should look at socket status. The first is a ConnectionRefusedError. If a client produces this error, it means that the server is probably running, but not accepting connections from the port, or maybe the host, that the client used. So you should check the status of the socket on the server to see if it’s active. You could also get a Connection timed out error.

02:35 This suggests that perhaps there’s a firewall not permitting a connection to that socket, or there could be some other connectivity issues.

02:46 You’ll learn about connectivity issues and other issues dealing with communication in the next lesson.

Become a Member to join the conversation.