SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Simple Chat Room using Python
This article demonstrates – 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.
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 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 server to client takes place along individual
threads based on socket objects created for the sake of 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.
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.
Usage
This server can be set up on a local area network by choosing
any on computer to be a server node, and using that computer’s
private IP address as the server IP address.
For example, if a local area network has a set of private IP
addresses assigned ranging from 192.168.1.2 to 192.168.1.100,
then any computer from these 99 nodes can act as a server, and
the remaining nodes may connect to the server node by using
the server’s private IP address. Care must be taken to choose a
port that is currently not in usage. For example, port 22 is default
for ssh, and port 80 is default for HTTP protocols. So these two
ports preferably, shouldnt be used or reconfigured to make them
free for usage.
However, if the server is meant to be accessible beyond a local
network, the public IP address would be required for usage. This
would require port forwarding in cases where a node from a local
network (node that isnt the router) wishes to host the server. In
this case, we would require any requests that come to the public
IP addresses to be re routed towards our private IP address in
our local network, and would hence require port forwarding.
To run the script, simply download it from the GitHub link
specified at the bottom of the post, and save it at a convenient
location on your computer.
Usage
This server can be set up on a local area network by choosing
any on computer to be a server node, and using that
computer’s private IP address as the server IP address.
For example, if a local area network has a set of private IP
addresses assigned ranging from 192.168.1.2 to
192.168.1.100, then any computer from these 99 nodes can
act as a server, and the remaining nodes may connect to the
server node by using the server’s private IP address. Care
must be taken to choose a port that is currently not in usage.
For example, port 22 is default for ssh, and port 80 is default
for HTTP protocols. So these two ports preferably, shouldn’t
be used or reconfigured to make them free for usage.
However, if the server is meant to be accessible beyond a
local network, the public IP address would be required for
usage. This would require port forwarding in cases where a
node from a local network (node that isnt the router) wishes to
host the server. In this case, we would require any requests
that come to the public IP addresses to be re routed towards
our private IP address in our local network, and would hence
require port forwarding.
To run the script, simply download it from the Get Hub link
specified at the bottom of the post, and save it at a convenient
location on your computer.
/* Both the server and client script can then be run
from the Command prompt (in Windows) or from bash
Terminal (Linux users) by simply typing
"python chat_server.py " or "python client.py ".
For example, */
python chat_server.py 192.168.55.13 8081
python client.py 192.168.55.13 8081
Below is the Server side script that must be run at all times to
keep the chat room running.
Filter none
edit
play_ arrow
brightness_4
# Python program to implement server side of chat room.
import socket
import select
import sys
from thread import *
"""The first argument AF_INET is the address domain of the
socket. This is used when we have an Internet Domain with
any two hosts The second argument is the type of socket.
SOCK_STREAM means that data or characters are read in
a continuous flow."""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# checks whether sufficient arguments have been provided
if len(sys.argv) != 3:
print "Correct usage: script, IP address, port number"
exit()
# takes the first argument from command prompt as IP address
IP_address = str(sys.argv[1])
# takes second argument from command prompt as port number
Port = int(sys.argv[2])
"""
binds the server to an entered IP address and at the
specified port number.
The client must be aware of these parameters
"""
server.bind((IP_address, Port))
"""
listens for 100 active connections. This number can be
increased as per convenience.
"""
server.listen(100)
list_of_clients = []
def clientthread(conn, addr):
# sends a message to the client whose user object is conn
conn.send("Welcome to this chatroom!")
while True:
try:
message = conn.recv(2048)
if message:
"""prints the message and address of the
user who just sent the message on the server
terminal"""
print "<" + addr[0] + "> " + message
# Calls broadcast function to send message to all
message_to_send = "<" + addr[0] + "> " + message
broadcast(message_to_send, conn)
else:
"""message may have no content if the connection
is broken, in this case we remove the connection"""
remove(conn)
except:
continue
"""Using the below function, we broadcast the message to all
clients who's object is not the same as the one sending
the message """
def broadcast(message, connection):
for clients in list_of_clients:
if clients!=connection:
try:
clients.send(message)
except:
clients.close()
# if the link is broken, we remove the client
remove(clients)
"""The following function simply removes the object
from the list that was created at the beginning of
the program"""
def remove(connection):
if connection in list_of_clients:
list_of_clients.remove(connection)
while True:
"""Accepts a connection request and stores two parameters,
conn which is a socket object for that user, and addr
which contains the IP address of the client that just
connected"""
conn, addr = server.accept()
"""Maintains a list of clients for ease of broadcasting
a message to all available people in the chatroom"""
list_of_clients.append(conn)
# prints the address of the user that just connected
print addr[0] + " connected"
# creates and individual thread for every user
# that connects
start_new_thread(clientthread,(conn,addr))
conn.close()
server.close()
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 users 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.
filter_ none
edit
play_ arrow
brightness_4
# Python program to implement client side of chat room.
import socket
import select
import sys
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
print "Correct usage: script, IP address, port number"
exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))
while True:
# maintains a list of possible input streams
sockets_list = [sys.stdin, server]
""" There are two possible input situations. Either the
user wants to give manual input to send to other people,
or the server is sending a message to be printed on the
screen. Select returns from sockets_list, the stream that
is reader for input. So for example, if the server wants
to send a message, then the if condition will hold true
below.If the user wants to send a message, the else
condition will evaluate as true"""
read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
for socks in read_sockets:
if socks == server:
message = socks.recv(2048)
print message
else:
message = sys.stdin.readline()
server.send(message)
sys.stdout.write("<You>")
sys.stdout.write(message)
sys.stdout.flush()
server.close()
Output: In the picture given below, a server has been initialized
on the left side of the terminal and a client script on the right side
of the terminal.
(Splitting of terminal done using tmux, ‘sudo apt-get install
tmux’). For
initialization purposes, you can see that whenever a message is
sent by a user, the message along with IP address is shown on
the server side.
The below picture has a basic conversation between two people
on the same server. Multiple clients can connect to the server in
the same way,
Simple chat room using python

More Related Content

What's hot (20)

PDF
IoT with Python
Dr. Sanjay Shitole
 
PPTX
Socket programming
Ujjwal Kumar
 
PPTX
Ipv4 presentation
shakeel khan
 
PPT
Snmp
hetaljadav
 
PPTX
Routing protocols
rajshreemuthiah
 
PPTX
IP Addressing & subnetting strategy
Mustafa Salam
 
PPT
Application layer protocols
JUW Jinnah University for Women
 
PDF
Introduction to Internet of Things.pdf
GVNSK Sravya
 
PPTX
HyperText Transfer Protocol (HTTP)
Gurjot Singh
 
PPTX
DHCP & DNS
NetProtocol Xpert
 
DOCX
Social Networking Project (website) full documentation
Tenzin Tendar
 
PPTX
IPV6 ADDRESS
Jothi Lakshmi
 
PPT
Basic network training1
Arunchai Seangparch
 
PDF
How to create a chat application on Android platform?
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
IP Spoofing
Akmal Hussain
 
PPTX
Http protocol
Arpita Naik
 
PPTX
Mac addresses(media access control)
Ismail Mukiibi
 
PPTX
Simple Mail Transfer Protocol
Ujjayanta Bhaumik
 
PPTX
OSI Model
Simran Kaur
 
PPTX
Business models for business processes on IoT
FabMinds
 
IoT with Python
Dr. Sanjay Shitole
 
Socket programming
Ujjwal Kumar
 
Ipv4 presentation
shakeel khan
 
Routing protocols
rajshreemuthiah
 
IP Addressing & subnetting strategy
Mustafa Salam
 
Application layer protocols
JUW Jinnah University for Women
 
Introduction to Internet of Things.pdf
GVNSK Sravya
 
HyperText Transfer Protocol (HTTP)
Gurjot Singh
 
DHCP & DNS
NetProtocol Xpert
 
Social Networking Project (website) full documentation
Tenzin Tendar
 
IPV6 ADDRESS
Jothi Lakshmi
 
Basic network training1
Arunchai Seangparch
 
How to create a chat application on Android platform?
baabtra.com - No. 1 supplier of quality freshers
 
IP Spoofing
Akmal Hussain
 
Http protocol
Arpita Naik
 
Mac addresses(media access control)
Ismail Mukiibi
 
Simple Mail Transfer Protocol
Ujjayanta Bhaumik
 
OSI Model
Simran Kaur
 
Business models for business processes on IoT
FabMinds
 

Similar to Simple chat room using python (20)

DOCX
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
kacie8xcheco
 
DOCX
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
hanneloremccaffery
 
PPTX
Client server chat application
Samsil Arefin
 
PPTX
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
RashidFaridChishti
 
PPTX
A.java
JahnaviBhagat
 
PPT
Networking Java Socket Programming
Mousmi Pawar
 
DOCX
692015 programming assignment 1 building a multi­threaded w
smile790243
 
PPT
Md13 networking
Rakesh Madugula
 
DOCX
1)Building a MultiThreaded Web ServerIn this lab we will devel
AgripinaBeaulieuyw
 
DOCX
Application programming interface sockets
Kamran Ashraf
 
PPTX
Chat server nitish nagar
Nitish Nagar
 
DOCX
Mail Server Project Report
Kavita Sharma
 
DOC
Socket
Amandeep Kaur
 
PDF
How a network connection is created A network connection is initi.pdf
arccreation001
 
PPTX
Java Network Programming.pptx
RoshniSundrani
 
DOCX
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
VannaSchrader3
 
DOCX
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
alfredacavx97
 
PPT
Unit 8 Java
arnold 7490
 
PPT
java networking
Waheed Warraich
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
kacie8xcheco
 
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
hanneloremccaffery
 
Client server chat application
Samsil Arefin
 
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
RashidFaridChishti
 
Networking Java Socket Programming
Mousmi Pawar
 
692015 programming assignment 1 building a multi­threaded w
smile790243
 
Md13 networking
Rakesh Madugula
 
1)Building a MultiThreaded Web ServerIn this lab we will devel
AgripinaBeaulieuyw
 
Application programming interface sockets
Kamran Ashraf
 
Chat server nitish nagar
Nitish Nagar
 
Mail Server Project Report
Kavita Sharma
 
How a network connection is created A network connection is initi.pdf
arccreation001
 
Java Network Programming.pptx
RoshniSundrani
 
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
VannaSchrader3
 
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
alfredacavx97
 
Unit 8 Java
arnold 7490
 
java networking
Waheed Warraich
 
Ad

More from VISHAL VERMA (20)

DOCX
srml hr hr sec school
VISHAL VERMA
 
DOC
Project report
VISHAL VERMA
 
DOCX
The text for the handwritten declaration sbi
VISHAL VERMA
 
DOCX
Oriental academy hr school
VISHAL VERMA
 
DOCX
Luthra academy school
VISHAL VERMA
 
DOCX
Cytotoxic reaction
VISHAL VERMA
 
DOCX
Sugar industry in india
VISHAL VERMA
 
PPTX
Met gala 2017 dresses
VISHAL VERMA
 
PPTX
Met gala 2017 dresses 3
VISHAL VERMA
 
PPTX
Met gala 2017 dresses 2
VISHAL VERMA
 
DOCX
Chemistry project food adulteration
VISHAL VERMA
 
DOCX
Caffiene
VISHAL VERMA
 
DOCX
Mansar lake
VISHAL VERMA
 
PPTX
Learning
VISHAL VERMA
 
PPTX
Attitude
VISHAL VERMA
 
PPTX
Organiztional behaviour
VISHAL VERMA
 
PPTX
Busniess ethics
VISHAL VERMA
 
DOCX
Shahnaz hussain
VISHAL VERMA
 
DOCX
Electrochemical cell
VISHAL VERMA
 
DOCX
Antibiotics
VISHAL VERMA
 
srml hr hr sec school
VISHAL VERMA
 
Project report
VISHAL VERMA
 
The text for the handwritten declaration sbi
VISHAL VERMA
 
Oriental academy hr school
VISHAL VERMA
 
Luthra academy school
VISHAL VERMA
 
Cytotoxic reaction
VISHAL VERMA
 
Sugar industry in india
VISHAL VERMA
 
Met gala 2017 dresses
VISHAL VERMA
 
Met gala 2017 dresses 3
VISHAL VERMA
 
Met gala 2017 dresses 2
VISHAL VERMA
 
Chemistry project food adulteration
VISHAL VERMA
 
Caffiene
VISHAL VERMA
 
Mansar lake
VISHAL VERMA
 
Learning
VISHAL VERMA
 
Attitude
VISHAL VERMA
 
Organiztional behaviour
VISHAL VERMA
 
Busniess ethics
VISHAL VERMA
 
Shahnaz hussain
VISHAL VERMA
 
Electrochemical cell
VISHAL VERMA
 
Antibiotics
VISHAL VERMA
 
Ad

Recently uploaded (20)

PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 

Simple chat room using python

  • 1. Simple Chat Room using Python This article demonstrates – 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. 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
  • 2. has a socket associated with the same port can communicate with the server socket. Multi-Threading A thread is 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 server to client takes place along individual threads based on socket objects created for the sake of 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. 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. Usage This server can be set up on a local area network by choosing any on computer to be a server node, and using that computer’s private IP address as the server IP address. For example, if a local area network has a set of private IP addresses assigned ranging from 192.168.1.2 to 192.168.1.100,
  • 3. then any computer from these 99 nodes can act as a server, and the remaining nodes may connect to the server node by using the server’s private IP address. Care must be taken to choose a port that is currently not in usage. For example, port 22 is default for ssh, and port 80 is default for HTTP protocols. So these two ports preferably, shouldnt be used or reconfigured to make them free for usage. However, if the server is meant to be accessible beyond a local network, the public IP address would be required for usage. This would require port forwarding in cases where a node from a local network (node that isnt the router) wishes to host the server. In this case, we would require any requests that come to the public IP addresses to be re routed towards our private IP address in our local network, and would hence require port forwarding. To run the script, simply download it from the GitHub link specified at the bottom of the post, and save it at a convenient location on your computer. Usage This server can be set up on a local area network by choosing any on computer to be a server node, and using that computer’s private IP address as the server IP address. For example, if a local area network has a set of private IP addresses assigned ranging from 192.168.1.2 to 192.168.1.100, then any computer from these 99 nodes can act as a server, and the remaining nodes may connect to the server node by using the server’s private IP address. Care must be taken to choose a port that is currently not in usage. For example, port 22 is default for ssh, and port 80 is default for HTTP protocols. So these two ports preferably, shouldn’t be used or reconfigured to make them free for usage. However, if the server is meant to be accessible beyond a local network, the public IP address would be required for usage. This would require port forwarding in cases where a node from a local network (node that isnt the router) wishes to host the server. In this case, we would require any requests
  • 4. that come to the public IP addresses to be re routed towards our private IP address in our local network, and would hence require port forwarding. To run the script, simply download it from the Get Hub link specified at the bottom of the post, and save it at a convenient location on your computer. /* Both the server and client script can then be run from the Command prompt (in Windows) or from bash Terminal (Linux users) by simply typing "python chat_server.py " or "python client.py ". For example, */ python chat_server.py 192.168.55.13 8081 python client.py 192.168.55.13 8081 Below is the Server side script that must be run at all times to keep the chat room running. Filter none edit play_ arrow brightness_4 # Python program to implement server side of chat room. import socket import select import sys from thread import * """The first argument AF_INET is the address domain of the socket. This is used when we have an Internet Domain with any two hosts The second argument is the type of socket. SOCK_STREAM means that data or characters are read in a continuous flow.""" server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # checks whether sufficient arguments have been provided if len(sys.argv) != 3: print "Correct usage: script, IP address, port number" exit() # takes the first argument from command prompt as IP address IP_address = str(sys.argv[1])
  • 5. # takes second argument from command prompt as port number Port = int(sys.argv[2]) """ binds the server to an entered IP address and at the specified port number. The client must be aware of these parameters """ server.bind((IP_address, Port)) """ listens for 100 active connections. This number can be increased as per convenience. """ server.listen(100) list_of_clients = [] def clientthread(conn, addr): # sends a message to the client whose user object is conn conn.send("Welcome to this chatroom!") while True: try: message = conn.recv(2048) if message: """prints the message and address of the user who just sent the message on the server terminal""" print "<" + addr[0] + "> " + message # Calls broadcast function to send message to all message_to_send = "<" + addr[0] + "> " + message broadcast(message_to_send, conn) else: """message may have no content if the connection is broken, in this case we remove the connection""" remove(conn) except: continue """Using the below function, we broadcast the message to all clients who's object is not the same as the one sending the message """ def broadcast(message, connection): for clients in list_of_clients: if clients!=connection: try: clients.send(message) except: clients.close() # if the link is broken, we remove the client remove(clients)
  • 6. """The following function simply removes the object from the list that was created at the beginning of the program""" def remove(connection): if connection in list_of_clients: list_of_clients.remove(connection) while True: """Accepts a connection request and stores two parameters, conn which is a socket object for that user, and addr which contains the IP address of the client that just connected""" conn, addr = server.accept() """Maintains a list of clients for ease of broadcasting a message to all available people in the chatroom""" list_of_clients.append(conn) # prints the address of the user that just connected print addr[0] + " connected" # creates and individual thread for every user # that connects start_new_thread(clientthread,(conn,addr)) conn.close() server.close() 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 users 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. filter_ none edit play_ arrow brightness_4
  • 7. # Python program to implement client side of chat room. import socket import select import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if len(sys.argv) != 3: print "Correct usage: script, IP address, port number" exit() IP_address = str(sys.argv[1]) Port = int(sys.argv[2]) server.connect((IP_address, Port)) while True: # maintains a list of possible input streams sockets_list = [sys.stdin, server] """ There are two possible input situations. Either the user wants to give manual input to send to other people, or the server is sending a message to be printed on the screen. Select returns from sockets_list, the stream that is reader for input. So for example, if the server wants to send a message, then the if condition will hold true below.If the user wants to send a message, the else condition will evaluate as true""" read_sockets,write_socket, error_socket = select.select(sockets_list,[],[]) for socks in read_sockets: if socks == server: message = socks.recv(2048) print message else: message = sys.stdin.readline() server.send(message) sys.stdout.write("<You>") sys.stdout.write(message) sys.stdout.flush() server.close() Output: In the picture given below, a server has been initialized on the left side of the terminal and a client script on the right side of the terminal.
  • 8. (Splitting of terminal done using tmux, ‘sudo apt-get install tmux’). For initialization purposes, you can see that whenever a message is sent by a user, the message along with IP address is shown on the server side. The below picture has a basic conversation between two people on the same server. Multiple clients can connect to the server in the same way,