Building a chat app using sockets in python

Hey guys

In this tutorial I will guide you to building a command-line chatting app in Python using sockets, If you had never had previous experience with sockets worry out I will explain everything detailed.

What are sockets?

sockets are two endpoints that can talk to each other ranging from the same on a machine or on wireless devices that are far away from each other.

Socket programming is a way of making two nodes on a network to communicate with each other. One socket listens on a particular port at an IP( server ), while another socket reaches out to the other to form a connection( client ).

The socket programming is the backbone of the internet system itself, deep down all these communication, we see now days arise from point to point communication.

Python socket module comes by default with the Python standard library therefore you don’t need to install anything to get started.

Structure of our Project

As we have seen above sockets are nodes that talk to each other, and in our project, we will implement two nodes using sockets one as a client and the other as a server.

Those two nodes will be sending messages two to each other as normal chatting applications.

Python socket programming

Getting started with sockets is very easy, you can use socket in various bidirectional communication, for instance, we can use them to get the IP from the hostname as shown below

Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket 
>>> socket.gethostname() #getting our local host name
'kalebu-PC'
>>> socket.gethostbyname('www.google.com') #getting IP of Google 
'172.217.170.4'

As we can see in the above example we were able to get our local hostname using **gethostname **method and also we were able to get the Ip address of Google using gethostbyname method.

Building our First Socket(node)

There are  many communication protocols for bi-directional communication it all comes out to out to where is  being applied.

Example of communication protocols include Transmission communication Protocol (TCP), User Datagram Protocol (UDP), File Transfer Protocol (FTP) and etc

On building our Chatting application we are going to use Transmission communication protocol (TCP), knowing how it works will be appreciated but you don't really need to.

Let's create our first node

On creating a socket(node) in python for TCP we are going to pass to the main parameters which are

  • socket.AF_INET means socket belong to IPV4 Family
  • socket.SOCK_STREAM means connection configured using TCP Protocol

Write the following on your python shell to create your first node in python

>>> import socket 
>>> node = socket.socket(socket.AF_INET, socket.SOCK_SaTREAM)

Congratulations you have just your first node in python, now let’s proceed to really building our application.

We need to have two separate node on two python scripts whereby one node will serve as a server and the other one as a client.  Now Let's begin building our server node

Building Server node

I’m a great fan of object *oriented programming ( OPP ) , I used a class to abstractly define my server node with implemented method to recieve and send communication from the client.

Note

During listening our server is waiting, therefore our code won't receive multiple SMS send from client to tackle this we need to apply multithreading, therefore I did the same to receiving method so as it can keep receiving independently without blocking the our app

Below is a complete code for our server script , I'm gonna explain in details later  now just proof read it

import socket 
import threading
​
class ServerNode:
    def __init__(self):
        self.node = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        port_and_ip = ('127.0.0.1', 12345)
        self.node.bind(port_and_ip)
        self.node.listen(5)
        self.connection, addr = self.node.accept()
​
    def send_sms(self, SMS):
        self.connection.send(SMS.encode())
​
    def receive_sms(self):
        while True:
            data = self.connection.recv(1024).decode()
            print(data)
​
    def main(self):
        while True:
            message = input()
            self.send_sms(message)
​
server = ServerNode()
always_receive = threading.Thread(target=server.receive_sms)
always_receive.daemon = True
always_receive.start()
server.main()

Code Explanation

Binding Ip address and port

port_and_ip = ('127.0.0.1', 12345)
self.node.bind(port_and_ip)

Binding* Ip addres*s and port to our node it’s like we are declaring in house of Ip address in the room of port that’s where our server node lives , therefore client have to come to this room to connect.

Which means client node must mention exactly server ip and port in order to connect.

In this demo we ran both two separate scripts on single machine, but the code also works over two seprate PC you just need to be on the same network.

Listening to Incoming connection

self.node.listen(5)
self.connection, addr = self.node.accept()

The above server code,It tells the server node to listen to incoming connections from a client  and then accept connection when connection is found .

send_sms ( ) method

def send_sms(self, SMS):
    self.connection.send(SMS.encode())

This method with only one line of code accept the SMS as parameter and send it to the client node, note that in order to send SMS using sockets it have to be in binary thus why we have encoded it .

receive_sms ( ) method

def receive_sms(self):
    while True:
        data = self.connection.recv(1024).decode()
        print(data)

This method is used to **receive the message from the client through out the program life, we have ** threaded it so as to prevent the app from freezing.

Main ( ) method

def main(self):
    while True:
        message = input()
        self.send_sms(message)

This method is used to prompt a message from server and send it to clients

Finishing things up

server = ServerNode()
always_receive = threading.Thread(target=server.receive_sms)
always_receive.daemon = True
always_receive.start()
server.main()

The above 5 lines of code are for creating a server instance and then run it by threading the receiving method

Building Client Node

Almost everything on Server node is similar to client node except the fact that client node connect to server instead of listening from connection , therefore big difference is seen on Constructor but Everything is almost the same.

import socket 
import threading
​
class ClientNode:
    def __init__(self):
        self.node = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        port_and_ip = ('127.0.0.1', 12345)
        self.node.connect(port_and_ip)
​
    def send_sms(self, SMS):
        self.node.send(SMS.encode())
​
    def receive_sms(self):
        while True:       
            data = self.node.recv(1024).decode()
            print(data)
​
    def main(self):
        while True:
            message = input()
            self.send_sms(message)
​
Client = ClientNode()
always_receive = threading.Thread(target=Client.receive_sms)
always_receive.daemon = True
always_receive.start()
Client.main()

Once you run the code the output will be something as below, please make sure to start running a Server.py before running Client.py.

Well, that's all, If you did everything as shown above your two nodes should up running ready to send each other messages.

Follow me on twitter

Kalebu Jordan
I am a Mechatronics engineer and professional Python Developer passionate about innovation, opensource and technical writing, Here I will be writing mostly on Python, DevOps, AI/ML and security
Dar es Salaam, Tanzania