Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following python code is for a client/server program chat room. Both the server and client side work, however, the program also needs to include

The following python code is for a client/server program chat room. Both the server and client side work, however, the program also needs to include two more features: (1) a warning message from the server to the client if users use an inappropriate word in the chatroom, or (2) if there is a taboo word entered (a word that goes over the line), the system will exit for that particular user. The basic logic for these conditions are listed below, but it is still unclear how to include this within the server side of the program which is listed also. I have include the client side code, if this is needed as well.

========Conditions======== swear = ("hate", "oppression") tooFarSwear = ("taboo") while True: userInput = str.lower(input("Tell me about your day: ")) if any(userInput.find(s) >= 0 for s in swear): print("WARNING: Hey, take it easy") if tooFarSwear in userInput: exit() 

==============server code===========

# 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============

# 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("")

sys.stdout.write(message)

sys.stdout.flush()

server.close()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

More Books

Students also viewed these Databases questions

Question

which single analysis tool provides a visual representation of the

Answered: 1 week ago

Question

how to calculate linear density of a simple cubic crystal system

Answered: 1 week ago

Question

When is it appropriate to use a root cause analysis

Answered: 1 week ago

Question

2. Identify conflict triggers in yourself and others

Answered: 1 week ago