Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
53 views

Python Project

This document describes a project to build a command line interface (CLI) based chat application in Python. The application uses a client-server architecture with socket programming and multithreading to allow multiple clients to connect to a central server and communicate in real-time across different chat rooms. The server script establishes a socket to listen for client connections while spawning a new thread for each client. The client script connects to the server socket and allows sending and receiving messages. Screenshots of the working application are provided, along with links to the GitHub source code repository and references used.

Uploaded by

Shaurya Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Python Project

This document describes a project to build a command line interface (CLI) based chat application in Python. The application uses a client-server architecture with socket programming and multithreading to allow multiple clients to connect to a central server and communicate in real-time across different chat rooms. The server script establishes a socket to listen for client connections while spawning a new thread for each client. The client script connects to the server socket and allows sending and receiving messages. Screenshots of the working application are provided, along with links to the GitHub source code repository and references used.

Uploaded by

Shaurya Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Submitted by

Tejaswi G : 12100200
Shivanshu Nagvanshi : 12100668
Shivam Upadhyay : 12100675

mini Project : Python Programming


int 213

toPic : CLI (Command Line Interface) based chat application


introduction
Chat tools enable users to start chatting with other users in real-
time. It also enables users to transmit text messages, images,
videos, and hyperlinks. In this project, we aim to build a simple
command-line chat tool which is easy to use and also has a very
minimal interface.
Python is one of the most versatile programming languages
and one can observe that through its various applications
everywhere.
What iS a chat room?
A chat room is a medium/interface that allows two or more
people to chat and send messages to everyone. It can be
interpersonal (one-one) and group chat too. In this tutorial,
we’re going to build a group chat room that can host more
than two clients at a time.
architecture
For chat room, we’re going to use the server-client architecture.
It means multiple clients will be hosted by one server.
objective of the Project
Building a chat tool with a simple command-line interface
which supports multiple chat rooms.
Primary goalS
Understand socket programming how it can be used to build
an application.
Understand multithreading and how it helps a program to
maximum utilize the CPU time.
Build a fully functioning chat tool that allows multiple users to
communicate with each other.
Incorporate multiple chat rooms (or groups) functionality into
the chat tool.
How to set up a simple Chat Room server and allow multiple
clients to connect to it using a client-side script. The code uses
the concept of sockets and threading.
deSign:
data Structure uSed
Socket Programming
Sockets can be thought of as endpoints in a communication
channel that is bi-directional and establishes communication
between a server and one or more clients. Here, we set up a
socket on each end and allow a client to interact with other
clients via the server. The socket on the server side associates
itself with some hardware port on the server-side. Any client
that has a socket associated with the same port can
communicate with the server socket.
multi-threading
A thread is a sub-process that runs a set of commands
individually of any other thread. So, every time a user connects
to the server, a separate thread is created for that user, and
communication from the server to the client takes place along
individual threads based on socket objects created for the sake
of the identity of each client.
We will require two scripts to establish this chat room. One to
keep the serving running, and another that every client should
run in order to connect to the server.
databaSe management
Server Side ScriPt
The server-side script will attempt to establish a socket and bind
it to an IP address and port specified by the user (windows
users might have to make an exception for the specified port
number in their firewall settings, or can rather use a port that is
already open). The script will then stay open and receive
connection requests and will append respective socket objects
to a list to keep track of active connections. Every time a user
connects, a separate thread will be created for that user. In
each thread, the server awaits a message and sends that
message to other users currently on the chat. If the server
encounters an error while trying to receive a message from a
particular thread, it will exit that thread.
client-Side ScriPt
The client-side script will simply attempt to access the server
socket created at the specified IP address and port. Once it
connects, it will continuously check as to whether the input
comes from the server or from the client, and accordingly
redirects output. If the input is from the server, it displays the
message on the terminal. If the input is from the user, it sends
the message that the user enters to the server for it to be
broadcasted to other users.
This is the client-side script, that each user must use in order to
connect to the server.
algorithm or PSeudo code:
Server Side:
from base64 import encode
from tarfile import ENCODING
import threading
import socket

host="127.0.0.1"
port=55555
ENCODE="ascii"
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((host,port))
server.listen()

clients = [] #new clients connecting to the server


nicknames = [] #inckname of the clients

def broadcast(message): #broadcast message from server to


all the clients including this server/client
for client in clients:
client.send(message)

def handle(client): #handling 1 client


while True:
try:
message=client.recv(1024)
broadcast(message)
except: #client disconnects
index=clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
broadcast(f"{nickname} left the
chat!".encode(ENCODING))
nicknames.remove(nickname)
break
def receive():
while True:
client, address=server.accept() #loop waits
here till server accepts connection request from client
print(f"Connected with {str(address)}")
client.send('TEJA'.encode(ENCODING))
nickname = client.recv(1024).decode(ENCODING)

nicknames.append(nickname)
clients.append(client)

print(f"Nickname of the client is {nickname}!")


broadcast(f'{nickname} joined the
chat'.encode(ENCODING))
client.send('Connected to the server!'.encode(ENCODING))

thread =
threading.Thread(target=handle,args=(client,)) #one thread
created for each client
thread.start()
print("Server is listening....")
server_thread=threading.Thread(target=receive)
server_thread.start()

client Side :
from tarfile import ENCODING
import threading
import socket

nickname=input("Enter nickname : ")

ENCODING="ascii"
client= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1', 55555))
def receive():
while True:
try:
message=client.recv(1024).decode(ENCODING)
if message == 'TEJA':
client.send(nickname.encode(ENCODING))
else:
print(message)
except:
print("An error occured!")
client.close()
break
def write():
while True:
message= f'{nickname}: {input("")}'
client.send(message.encode(ENCODING))

receive_thread=threading.Thread(target=receive)
receive_thread.start()

write_thread=threading.Thread(target=write)
write_thread.start()
reSult:
ScreenShotS of the reSult :
concluSion and referenceS:
GitHub links:
https://tejaswi0g.github.io/cli-based-chat-application/
https://github.com/TEJASWI0G/cli-based-chat-application

Conclusion & References


https://www.geeksforgeeks.org/socket-programming-python/
folder to Python file:
Click here

You might also like