Using Python to check TCP Socket Connectivity

Sudhendu
3 min readJun 20, 2020
Socketing in the age of deep learning. Source: https://www.sapiens.org/column/curiosities/stone-age-myths/

As I am progressing towards finishing my courses on Python, I am doing some hands-on to make sure I practice the skills I am learning.

To do this, I am constantly looking for problems in my day-to-day work which I can help automate or better using my learning of Python. Here is one.

Problem Statement: To do telnet check for multiple machines over multiple posts over multiple times.

Background: As part of my work at BCT TIBCO, I am responsible for the installation and the configuration of TIBCO product stacks on our client's infrastructure. Here is how a typical workflow goes:

  1. The client provides us with a bunch of machines, windows, and Unix based.
  2. We request for a certain list of ports (both software level, ex 22, and application-specific).
  3. The client sends this request to their network team, which goes into multiple hands before coming back to us, saying it is ‘DONE’
  4. We check and nope, it is not done. Some connectivity is failing.
  5. Repeat 2,3,4 for a couple of times, before calling it done.

Solution: I created a small python code to help streamline this entire process and make it a single click!

Here is the link to GitHub repo if you want to skip the reading (the readme at GitHub explains everything).

Disclaimer: I know this is not the most efficient way (can be done using BAT or scripting), and also that there are many opensource, free tools available online which does the same. My reason for making this is the same reason why couples make kids, to create something of their own even though there are million other kids on the planet ;) )

Usage Guide:

  1. Input: Can take a CSV file with columns: IP and Port.

2. Output: It runs the port checker on all these IP-Port combinations and creates an output file with a new column ‘Status’:

3. Structure: This code is packaged into a .exe and hence can run standalone (check tools folder).

exe: port_checker.exe

file: input/in_server_port.csv

4. Source Code: The source code is placed in /src/ folder. It has the python code along with all dependencies.

5. Usage: Download the tools folders. Change the input file as mentioned above. Open the command prompt and run the below command:

port_checker.exe

Python Code overview

The code is written into three functions and one main function.

Firstly, let us import the libraries. We will need pandas to help us read the CSV file, parse it and write the result CSV file back. The socket is used for networking related tasks.

Next, we define get_checklist(). This function reads the CSV file and gets the list of IPs and Ports. It also inserts a new column named ‘Status’ with value is null. This column will later be used to add our status.

The function check_server() takes two inputs, server address, and port. The code is self-explanatory, but we are trying to create a socket connection with the host and port and use try-except-finally block to check the port status and return the result.

The check_create_status() is the first function that gets called and internally calls the above two functions.

The main() function is the entry point for the Python code. It calls check_create_status.

Some general FAQs:

  1. I hope the .exe is not a virus?

A: It is not. If you are worried about it, please download the src folder and use the python script. The .exe is to make it easy for people without Python coding knowledge.

2. Why is the .exe so big?

A: It has to do with how Python creates the package. Since the .exe should run standalone on your machine, python bundles all the dependencies into it. This includes modules like NumPy, pandas, socket, etc. Hence the big file size.

3. This can be done using a simple shell or bat command. Why use Python for this?

A: There are better ways to do it of course. But I am studying Python and hence this is just me solving my day-to-day problem using the language.

4. How does will it take to run?

A: Depending on the number of entries you have in the input file, TCP socket connection can take some time. The code has a timeout set (40 seconds) for each entry.

--

--

Sudhendu

Mostly running, hiking. Snowflake Data Superhero. Quora Top Writer २०१४. Work @ kipi.bi. Views are my own।