Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone help me alter the following python code below, its for a chat server in python and im supposed be able to run a

can someone help me alter the following python code below, its for a chat server in python and im supposed be able to run a list of /commands

as you can see in the code I alread yhave the commands /help /join /quit and /list which dod the following

HELP Syntax: HELP

Requests the server to display the help file. This command is not formally defined in an RFC, but is in use by most major IRC daemons.

JOIN Syntax: JOIN []

Makes the client join the channels in the comma-separated list , specifying the passwords, if needed, in the comma-separated list .If the channel(s) do not exist then they will be created. Defined in RFC 1459.

LIST Syntax: LIST [ []]

Lists all channels on the server. If the comma-separated list is given, it will return the channel topics. If is given, the command will be forwarded to for evaluation. Defined in RFC 1459.

QUIT Syntax: QUIT []

Disconnects the user from the server.[32] Defined in RFC 1459.

All of which work perfectly but the problem im having is trying to add a few more commands, can someone help me add the following commands

PONG

PRIVMSG

RESTART

RULES

whcih do the following

PONG Syntax: PONG []

This command is a reply to the PING command and works in much the same way. Defined in RFC 1459.

PRIVMSG Syntax: PRIVMSG

Sends to , which is usually a user or channel. Defined in RFC 1459.

RESTART Syntax: RESTART

Restarts a server.It may only be sent by IRC operator. Defined in RFC 1459.

RULES Syntax: RULES

Requests the server rules. This command is not formally defined in an RFC, but is used by most[which?] major IRC daemons.

please help

--------------------------------------------------------------------------------

ChatServer.py

-----------------------------------------------------------------------------------

import socket import sys import threading import Channel class Server: SERVER_CONFIG = {"MAX_CONNECTIONS": 15} HELP_MESSAGE = """ > The list of commands available are:  /help - Show the instructions /join [channel_name] - To create or switch to a channel. /quit - Exits the program. /list - Lists all available channels. """.encode('utf8') def __init__(self, host=socket.gethostbyname('localhost'), port=50000, allowReuseAddress=True): self.address = (host, port) self.channels = {} # Channel Name -> Channel  self.channels_client_map = {} # Client Name -> Channel Name   try: self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error as errorMessage: sys.stderr.write("Failed to initialize the server. Error - %s ", str(errorMessage)) raise   if allowReuseAddress: self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: self.serverSocket.bind(self.address) except socket.error as errorMessage: sys.stderr.write('Failed to bind to ' + self.address + '. Error - %s ', str(errorMessage)) raise   def listen_thread(self, defaultGreeting=" > Welcome to our chat app!!! What is your name? "): while True: print("Waiting for a client to establish a connection ") clientSocket, clientAddress = self.serverSocket.accept() print("Connection established with IP address {0} and port {1} ".format(clientAddress[0], clientAddress[1])) self.welcome_client(clientSocket) clientThread = threading.Thread(target=self.client_thread, args=(clientSocket,)) clientThread.start() def start_listening(self): self.serverSocket.listen(Server.SERVER_CONFIG["MAX_CONNECTIONS"]) listenerThread = threading.Thread(target=self.listen_thread) listenerThread.start() listenerThread.join() def welcome_client(self, clientSocket): clientSocket.sendall(" > Welcome to our chat app!!! What is your name? ".encode('utf8')) def client_thread(self, clientSocket, size=4096): clientName = clientSocket.recv(size).decode('utf8') welcomeMessage = '> Welcome %s, type /help for a list of helpful commands. ' % clientName clientSocket.send(welcomeMessage.encode('utf8')) while True: chatMessage = clientSocket.recv(size).decode('utf8').lower() if not chatMessage: break   if '/quit' in chatMessage: self.quit(clientSocket, clientName) break  elif '/list' in chatMessage: self.list_all_channels(clientSocket) elif '/help' in chatMessage: self.help(clientSocket) elif '/join' in chatMessage: self.join(clientSocket, chatMessage, clientName) else: self.send_message(clientSocket, chatMessage + ' ' , clientName) clientSocket.close() def quit(self, clientSocket, clientName): clientSocket.sendall('/quit'.encode('utf8')) self.remove_client(clientName) def list_all_channels(self, clientSocket): if len(self.channels) == 0: chatMessage = " > No rooms available. Create your own by typing /join [channel_name] "  clientSocket.sendall(chatMessage.encode('utf8')) else: chatMessage = ' > Current channels available are:  '  for channel in self.channels: chatMessage += "  " + channel + ": " + str(len(self.channels[channel].clients)) + " user(s)"  chatMessage += " "  clientSocket.sendall(chatMessage.encode('utf8')) def help(self, clientSocket): clientSocket.sendall(Server.HELP_MESSAGE) def join(self, clientSocket, chatMessage, clientName): isInSameRoom = False   if len(chatMessage.split()) >= 2: channelName = chatMessage.split()[1] if clientName in self.channels_client_map: # Here we are switching to a new channel.  if self.channels_client_map[clientName] == channelName: clientSocket.sendall((" > You are already in channel: " + channelName).encode('utf8')) isInSameRoom = True  else: # switch to a new channel  oldChannelName = self.channels_client_map[clientName] self.channels[oldChannelName].remove_client_from_channel(clientName) # remove them from the previous channel   if not isInSameRoom: if not channelName in self.channels: newChannel = Channel.Channel(channelName) self.channels[channelName] = newChannel self.channels[channelName].clients[clientName] = clientSocket self.channels[channelName].welcome_client(clientName) self.channels_client_map[clientName] = channelName else: self.help(clientSocket) def send_message(self, clientSocket, chatMessage, clientName): if clientName in self.channels_client_map: self.channels[self.channels_client_map[clientName]].broadcast_message(chatMessage, clientName + ": ") else: chatMessage = """ > You are currently not in any channels:  Use /list to see a list of available channels. Use /join [channel name] to join a channels. """.encode('utf8') clientSocket.sendall(chatMessage) def remove_client(self, clientName): if clientName in self.channels_client_map: self.channels[self.channels_client_map[clientName]].remove_client_from_channel(clientName) del self.channels_client_map[clientName] print("Client: " + clientName + " has left ") def server_shutdown(self): print("Shutting down chat server. ") self.serverSocket.shutdown(socket.SHUT_RDWR) self.serverSocket.close() def main(): chatServer = Server() print(" Listening on port " + str(chatServer.address[1])) print("Waiting for connections... ") chatServer.start_listening() chatServer.server_shutdown() if __name__ == "__main__": main() 

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

Students also viewed these Databases questions

Question

Have you ever told a lie that you regretted?

Answered: 1 week ago

Question

Distinguish between formal and informal reports.

Answered: 1 week ago