GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Machine Learning
  • Android
  • Java
  • .Net Core
  • PHP
  • Top Languages Poll
  • Kotlin
  • .Net
  • C#
  • Python
  • JavaScript
  • CodingInfinite Staff
  • Data Visualization
  • Line Chart

This view

  • Original page
  • New address

GeezerVison is showing codinginfinite.com — Developing Chat Application in Python with Source Code

python chat application tutorials

The Chat Application is very common today offered either via a web application or mobile application. Learning to write a Chat Application is good for understanding many network communication concepts and can be useful to build other network applications. Chat Application provides communication between two parties i.e. sender and receiver. The sender is someone who initiates and send a message to other known as receiver; receiver at other end receives the message. The role of sender and receiver is not fixed and keep exchanging during communication, so in simple words, at a point, someone who sends the message is a sender and who receive the message is called receiver. In networking terms, sender and receiver are denoted as source and destination respectively.

Communication can be of many types depending upon the method of communication and the number of parties involved. Some of the scenarios are :

  1. Simplex or one-way communication: Only one party is able to send the message and other parties can only receive.
  2. Duplex or two-way communication: Both parties can send and receive messages.

Duplex communication is a common way of communication and can be one-to-one (simple chat) or many-to-many (chat room)

Generally, in real-world communication is done directly using voice in an ideal situation (distance between communicating parties, identification of parties)  where sender speak out and the intended receiver respond after listening. So, what is the medium of communication here? Indeed, it is the air which helps our voice to travel to the receiver and successful communication depends upon air (high wind and long-distance can cause trouble). In online or digital communication the role of air is played by network channel (coaxial cable, fiber optics, etc.) and communication is controlled by a server. A server is a program which regulates the communication between sender and receiver.

So, to create a Python Chat Application, one has to write a server program and client program/s (sender and receiver). Suppose, two parties Alice and Bob want to chat with each other and ask you to develop a chat application then being a developer you have to write a server program and a client program (different instance of the same program will be used by both Alice and Bob or even more users).

we will demonstrate the aforementioned scenario and will develop a Python Chat Application for Alice and Bob. Python has many modules which can help us to create network-related application, the socket is one of such popular default Python modules for low-level network programming. We will first list and explain the steps for server and client programs and then implement the same using Python.

Server script

Server program has all the logic to control and regulate the Chat, so most of the chat logic is implemented with a server program. So first step of communication is to identify the users, how server do this? In network communication, users are identified by a socket which is nothing but a combination of IP address and port address. So, for human understanding, Alice and Bob will be chatting but for a network, it is two sockets process which is sending and receiving bytes.  Steps involved in this process is as follows:

  1. Create socket
  2. Communicate the socket address
  3. Keep waiting for an incoming connection request/s
  4. Connect to client
  5. Receive the message
  6. Decode the destination user and select the socket
  7. Send a message to the intended client
  8. Keep repeating step 5 & 6 as per users wish
  9. Exit i.e. end the communication by terminating the connection

Here’s the code for server-chat.py

import socket,select

port = 12345
socket_list = []
users = {}
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('',port))
server_socket.listen(5)
socket_list.append(server_socket)
while True:
    ready_to_read,ready_to_write,in_error = select.select(socket_list,[],[],0)
    for sock in ready_to_read:
        if sock == server_socket:
            connect, addr = server_socket.accept()
            socket_list.append(connect)
            connect.send("You are connected from:" + str(addr))
        else:
            try:
                data = sock.recv(2048)
                if data.startswith("#"):
                    users[data[1:].lower()]=connect
                    print "User " + data[1:] +" added."
                    connect.send("Your user detail saved as : "+str(data[1:]))
                elif data.startswith("@"):
                    users[data[1:data.index(':')].lower()].send(data[data.index(':')+1:])
            except:
                continue

server_socket.close()

Client script

Client script is run by the user, so the same client code will be run by a different user but each will have a separate socket so they will have their unique communication channel. Client script uses to be thin because it has very less work i.e. it only connect with the server and send and receive messages. The steps involved in client script are:

  1. Create a unique client socket per instance/user
  2. Connect to the server with given socket address (IP and port)
  3. Send and receive messages
  4. Repeat step 3 as per configuration
  5. Close the connection

Here’s the Code for client-chat.py

import socket

client_socket = socket.socket()
port = 12345
client_socket.connect(('127.0.0.1',port))

#recieve connection message from server
recv_msg = client_socket.recv(1024)
print recv_msg

#send user details to server
send_msg = raw_input("Enter your user name(prefix with #):")
client_socket.send(send_msg)


#receive and send message from/to different user/s

while True:
    recv_msg = client_socket.recv(1024)
    print recv_msg
    send_msg = raw_input("Send your message in format [@user:message] ")
    if send_msg == 'exit':
        break;
    else:
        client_socket.send(send_msg)

client_socket.close()

In the above program, each user has to run the client script separately after the server script is running. Once the client program connects to the server the client has to register itself as a user by giving a username, so the rest of the communication will be done using the username.

Steps for running the sample Chat application:

1. Open a terminal and Run the server-chat.py

2. Open a new terminal and run client-chat.py

            a) Enter the username with a ‘#’ prefix. Example: #alice

            b) Now, send the message to a user by following the format @username:message. Example: @bob:Hello, Bob! This is alice

3. Repeat step 2 for other users. (Maximum 5 users is allowed with server configuration i.e. server_socket.listen(5)

Limitations:

This demo on Python Chat Application has limited capabilities like the user get the message only after both parties enter the message to each other. Proper user validation etc. is not done.

To Do:

  1. Test a simple cryptography feature.
  2. Add proper Authentication of the user.

Note: both scripts are based on various Internet sources.

I hope this article was helpful for you, please let me know in the comment section below.

Here’re some more Articles, you might be interested:

— Top 5 Python Web Frameworks to Learn

— Data Visualization in Python Using Simple Line Chart

— Here Are The Ten Best Programming Languages to learn

CodingInfinite Staff

Similar Posts

Database Constraints in SQLite using Python Code by Querying

This article is the continuation of the previous article in which we have seen how to create and query a simple database. In this article, we will learn the following: Database Constraints in SQLite (PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY) Querying Database We will follow the following relational schema to create the database. Create…

Data Visualizing from CSV Format to Chart using Python

In this article, we will download a data set from an online resource and create a working visualization of that data. As we have discussed in the previous article, the internet is being bombarded by lots of data each second. An incredible amount and variety of data can be found online. The ability to analyze…

Creating GUI Application in Python – TKINTER Tutorial

Graphical User Interface A GUI (pronounced like “gooey”) allows the user to interact with the operating system using graphical elements such as icons, buttons, dialog boxes, inputs, etc. The interactions with GUI are mostly done with the help of small windows called dialog boxes. One part of the user interface consists of hardware devices such…

Make A Command-line based Image Re-sizer in Python 3 using Pillow

Python is a general-purpose programming language. It is so popular in software developers and has a vast number of plugins available to play with. It has a huge community to support new learners. In this article, we will see one of its plugin called “Pillow”. Pillow is an Imaging Library used o work with images,…

Python SQLite Tutorial – Creating Simple Query with Code Example

In this article, we will start learning to work with a Database using Python. Every useful application either, it is a desktop application, mobile application, or web application, uses some sort of database for the storage and retrieval of the data. In our previous article, we saw that we can achieve data persistence using files…

File I/O in Python – Read and Write Files Tutorial

The simple Python programs we write do not use persistent memory. Variables and objects we create in such programs are stored in the volatile memory (RAM). The data is erased when the program is closed. Most of the time, the program needs the data for later use. It must be somehow stored somewhere in the…

One Comment

  1. Where is the user specific messaging implemented?

  2. why is the sock.recv hanging on windows 10

Comments are closed.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.