Question: May you help me implement the following in my code: - Allow Client to leave the system by implementing a command. Use the username passed
May you help me implement the following in my code:
Allow Client to leave the system by implementing a command. Use the username
passed to client.py to print username has left on all other connected Clients.
Allow Client to leave the system unexpectedly. Use the username passed to client.py
to print username has left on all other connected Clients
One of the connected clients disconnecting should not cause the server to crash
Client should be able to unicast messages to another client individually when there
are more than two clients in the system. They also should be able to change between broadcast and unicast
In case of a crash, attempt to close remaining connections and print an error messagein the terminals of all previous connected devices.
my code :
server.py:
import socket
import threading
import sys
def mainport:
host 'localhost'
server socket.socketsocketAFINET, socket.SOCKSTREAM
server.bindhost port
server.listen
clients
usernames
def broadcastmessage senderclientNone:
for client in clients:
if client senderclient:
client.sendmessage
def handleclientclient:
while True:
try:
message client.recv
broadcastmessageclient
except:
index clients.indexclient
clients.removeclient
client.close
username usernamesindex
broadcastfusername has left'.encode
usernames.removeusername
break
def receiveclient:
while True:
client, address server.accept
printfConnection coming from straddress
client.sendUsernameencode
username client.recvdecode
usernames.appendusername
clients.appendclient
printfusername has joined'
broadcastfusername has joined".encode
client.sendWelcome to the server'.encode
thread threading.Threadtarget handleclient argsclient
thread.start
receiveclient
if namemain:
if lensysargv:
printUsage: python server.py port
else:
mainintsysargv
client.py:
import socket
import threading
import sys
def mainusername hostname, port:
client socket.socketsocketAFINET, socket.SOCKSTREAM
try:
client.connecthostname port
except Exception as e:
printfCould not connect: e
def receivemesage:
while True:
try:
message client.recvdecode
if message 'Username':
client.sendusernameencode
else:
printmessage
except Exception as e:
printfError: e
client.close
break
def write:
while True:
message fusername: input
if message "command":
command inputfusernamecommand
if command exit:
client.sendexitencode
break
elif command.startswithbroadcast:
client.sendcommandencode
elif command.startswithunicast:
client.sendcommandencode
elif command files:
client.sendfilesencode
elif command.startswithdownload:
client.sendcommandencode
else:
printInvalid command."
client.close
else:
client.sendmessageencode
receivethread threading.Threadtarget receivemesage
receivethread.start
writethread threading.Threadtarget write
writethread.start
if namemain:
if lensysargv:
printUsage: python client.py usernamehostnameport
else:
mainsysargv sysargv intsysargv
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
