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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-40.png" data-lazy- height="21" src="data:image/svg xml,” width=”431″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-40.png" data-lazy- height="333" src="data:image/svg xml,” width=”549″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-40.png" data-lazy- height="57" src="data:image/svg xml,” width=”463″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image5-34.png" data-lazy- height="20" src="data:image/svg xml,” width=”459″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-39.png" data-lazy- height="362" src="data:image/svg xml,” width=”396″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image7-24.png" data-lazy- height="73" src="data:image/svg xml,” width=”484″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image6-31.png" data-lazy- height="89" src="data:image/svg xml,” width=”532″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image9-17.png" data-lazy- height="129" src="data:image/svg xml,” width=”478″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image8-19.png" data-lazy- height="20" src="data:image/svg xml,” width=”459″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image12-8.png" data-lazy- height="187" src="data:image/svg xml,” width=”328″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image10-11.png" data-lazy- height="88" src="data:image/svg xml,” width=”496″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image11-8.png" data-lazy- height="40" src="data:image/svg xml,” width=”484″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image13-8.png" data-lazy- height="129" src="data:image/svg xml,” width=”477″>

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.