Python

Python Socket Programming

Socket programming is a method of allowing two nodes within a network to interact with one another. One socket (node) reads on a certain port upon an IP address. Whereas, the former one connects with it. The client connects towards a server and the server creates the auditor socket. These are the true foundations of online surfing.

To put it simply, there is indeed a server as well as a client. Integrating the socket package and creating a basic socket are the first steps in socket programming. At the time of implementing this article, we have been using Ubuntu 20.04 Linux system. Here are some examples of Python Socket Programming.

Example 01: Connect Socket to Google

Our first example will be using a socket to connect with Google. Therefore, go to the application area and search “terminal” in the search bar to open. If you find it difficult to open, then simply use the “Ctrl+Alt+T” key and the terminal will be opened.

Now after the opening of a terminal, we need to create a new python supported file to add out python script in it for socket programming. Hence, we have created a file “test.py” using the “touch” command as follows:

$ touch test.py

Let’s navigate towards the home directory of our system to open the newly created file. After you have opened the file, add the below python script in it for connecting the socket to a Google. We have added python support in this file first. Then we have imported system library “sys” and “socket” library in it. After that, we have used a try statement to create a socket. In the 5th line, we have created an object for socket class which is used to create a socket with the stream. If the socket created gets done, it will print a success message. Otherwise, except statement will be executed showing the socket creation failed message.

After that, we have created a variable “port” to assign it a value of “80”. Another try statement has been used to get the IP address of the host with which we are connecting our socket e.g. Google, in our case.  Try statement is getting an IP address via a socket class function “gethostbyname”. If the IP retrieval is successful, the control will be given to the print statement located outside of the try-except body, and the success message will be displayed on the terminal. On the other hand, if IP retrieval gets unsuccessful, it will print an error message and quit the process. Save your file and leave it to navigate towards the terminal.

Let’s run this file via python3 support in the shell as beneath. You can see that the socket has been successfully connected to google as the success messages are presenting.

$ python3 test.py

Example 02: Simple Server-Client Connection

Let’s have another simple example of connecting server and client to each other via socket programming. Let’s first create a new file “Server.py” with python extension in your home directory using the below-presented command.

$ touch Server.py

Open a file and paste the below code into it. This code is importing socket library first and then creating a socket via object “s” followed by the success message. Then, we have mentioned the variable “port” to add port number and bind this port number with our socket object “s” followed by a successful binding message. We have put our socket to the listening mode by method “listen”. Then, we have created a forever while loop to establish a connection with the client via accept() method until we interrupt it or some error happens.

The variable “addr” represents the address of a client. We have declared the variable “var” with some message in it to send to the client. But before that, we have to encode byte-like characters. Hence, we have used encode() method to do so. We have used the “send()” method to send a message to the client and close the connection. Save your file and press cross sign to leave it.

Check whether our server is working properly and active or not. For that purpose, you will execute the file “Server.py” in the command shell terminal as follows:

$ python3 Server.py

It has created a socket successfully and it is listening properly. Now, to check whether it is working or not, we have to connect it with another host. Hence, we are going to use the “telnet” to do so.

Open another terminal window and write out the below query in it. You will see it will connect to the telnet while showing you a message of “Thank you for Connecting”. This means our server is properly working.

$ telnet localhost 12345

Come back to the server terminal. You can see it shows that your server has got a connection from some IP address.

Let’s create another file for client-side socket programming e.g. Client.py as follows:

$ touch Client.py

Open your file quickly from the home directory, and write the below code in it with no difference. We have imported the socket library and created a socket via the “socket” method. Port has been mentioned in the variable “port” and connection has been established via object “s” and method “connect with the port given. The “recv” method has been used to receive data from a server and then print it followed by the closing of the connection.

Let’s create a server socket first  and it will open the listening mode of the server as follows:

$ python Sevrer.py

Now, run the Client.py file using the below query. You can see it will output the message from a server host “Thankyou for Connecting”.

$ python3 Client.py

Let’s come to the server-side again. You can see it is showing that the server has got a connection from a client.

Conclusion:

We have completed all the important and required aspects of socket programming in this article. The examples contain the connection of a socket with google and other clients.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.