Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please finish the code after each #TODO in python , thank you. Introduction In the class, we learned the basic concepts of websocket and the
Please finish the code after each #TODO in python , thank you.
Introduction In the class, we learned the basic concepts of websocket and the basic usage of websocket. In this lab, we are going to implement a simple chatroom that can receive and send messages between between clients. Our chatroom contain two prat: server and client The server is able to be connected by clients(not only one), so you must use thread to deal with this situation. The client must be able to connect with the server according to the set ip and port. Server 1:127.0.0.1 port30000 Client Client CI Once the connection is established, the client must be able to read the string and send it to the server. Once the server receives the message from the client, it will broadcast the message to other connected clients. Server 127001 30000 Server 1:127.0.0. port 300 "Him "Hil" Hir Client Cleri Clu C Clari Client Server Once you finish this part, you have to create chatserver.py and copy following content in this file. In [ ]: Nimport socket import select import sys from threading import * import argparse class chatRoom: def init__(self, args) : # # TODO finish following variables # sever listens for 100 active connections #self.server #self.ip #self.port #self.name #self.list_of_clients def client_thread(self, conn, addr): # sends a message to the client whose user object is connected(conn) while True: conn. send (f"\033 [95mWelcome to {self.name} !\033[0m". encode()) message = str(conn.recv(2048), encoding='utf-8') if message: *# Display the received message and address of the source user message_to_send = ("\033[94m : \033(Om" + message).strip "print(message_to_send) # Calls broadcast function to send message to all self.broadcast (message_to_send, conn) else: # message may have no content if the connection is broken # In this case we remove the connection message_to_send = ("\033 (91m disconnected!\033[0m").stri "print(message_to_send) self.broadcast (message_to_send, conn) self.remove( conn) conn.close() break def broadcast (self, message, connection): ""Broadcast the message to all clients who's object is not the same as the one s # TODO def remove(self, connection): -"'"'Removes the object from the list that was created at the beginning of the prog # TODO def run(self): while True: try: "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 #TODO Maintains a list of clients for ease of broadcasting a message to all available people in the chatroom". # TODO # prints the address of the user that just connected "print(addr[0] + " connected") * # creates and individual thread for every user that connects *# TODO except KeyboardInterrupt: break try: conn.close() server.close(). except: pass if name __main__": prog "server.py" descr = "server side of chat room" parser = argparse. ArgumentParser(prog=prog, description=descr) parser. add_argument ("--ip", type=str, required=True) parser.add_argument ("--port", type=int, required=True) parser.add_argument("-n", "--name", default="socket chatroom", type=str) arguments = parser.parse_args() app = chatRoom(arguments) app.run() Client Part Once you finish, you have to create chatclient.py and copy following content in this file. In [ ] : import socket import select import sys import argparse def main(args): # create a "client" variable to connect to server # TODO while True: try: # maintains a list of possible input streams # TODO " 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" # TODO except KeyboardInterrupt: break client.close() if name == _main__": prog = "client.py" descr "client side of chat room" parser = argparse. ArgumentParser(prog=prog, description=descr) parser.add_argument("--ip", type=str, required=True) parser.add_argument("--port", type=int, required=True) arguments parser.parse_args() main(arguments)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