Question
The following is a Python program for a client and server chatroom. The program is to have two added features: (1) a warning should be
The following is a Python program for a client and server chatroom. The program is to have two added features: (1) a warning should be given from the server side if inappropriate language is used; and (2) if there is worse inappropriate language used, the connection for that client should automatically be gone. Sample code is given below for this; however, it needs to be included within the specific points in the program so that it runs correctly.
Added Features ==================== def checkSanity(message): response = 0 badWord = ("bad", "bad", "bad") worseWords = ("worse", "worst", "awful") while True: input_words = message.lower().split() for word in input_words: if word in badWord: response = 1 #set value 1 if word is in swear for word in input_words: if word in worseWords: remove(conn) # remove the connection if the message goes too far
#return the corresponding values based on the value of the response. works just like a switch case.
return { 0: "", 1: "watch your language"
}[response]
Use the function call for sanityCheck as shown below in clientthread fucntion of server code.
.........
# Calls broadcast function to send message to all
message_to_send = "<" + addr[0] + "> " + message
response = sanityCheck(message_to_send )
# check the respone from sanityCheck
if response!="" :
conn.send(response)
# check if the connection is closed .If not then broadcast the message
if conn in list_of_clients:
broadcast(message_to_send, conn)
=================
chatroom Server Code
==================
import socket def server_program(): # get the hostname host = socket.gethostname() port = 5000 # initiate port no above 1024 server_socket = socket.socket() # get instance # look closely. The bind() function takes tuple as argument server_socket.bind((host, port)) # bind host address and port together # configure how many client the server can listen simultaneously server_socket.listen(2) conn, address = server_socket.accept() # accept new connection print("Connection from: " + str(address)) while True: # receive data stream. it won't accept data packet greater than 1024 bytes data = conn.recv(1024).decode() if not data: # if data is not received break break print("from connected user: " + str(data)) data = input(' -> ') conn.send(data.encode()) # send data to the client conn.close() # close the connection if __name__ == '__main__': server_program()
===================
chatroom Client Code
===================
import socket def client_program(): host = socket.gethostname() # as both code is running on same pc port = 5000 # socket server port number client_socket = socket.socket() # instantiate client_socket.connect((host, port)) # connect to the server message = input(" -> ") # take input while message.lower().strip() != 'bye': client_socket.send(message.encode()) # send message data = client_socket.recv(1024).decode() # receive response print('Received from server: ' + data) # show in terminal message = input(" -> ") # again take input client_socket.close() # close the connection if __name__ == '__main__': client_program()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started