Beruflich Dokumente
Kultur Dokumente
class Server(object):
"""
The server class implements a server socket that can handle multiple client connections.
It is really important to handle any exceptions that may occur because other clients
are using the server too, and they may be unaware of the exceptions occurring. So, the
server must not be stopped when a exception occurs. A proper message needs to be show in
the
server console.
"""
MAX_NUM_CONN = 10 # keeps 10 clients in queue
def _bind(self):
"""
# TODO: bind host and port to this server socket
:return: VOID
"""
pass #remove this line after implemented.
def _listen(self):
"""
# TODO: puts the server in listening mode.
# TODO: if succesful, print the message "Server listening at ip/port"
:return: VOID
"""
try:
self._bind()
# your code here
except:
self.serversocket.close()
def _accept_clients(self):
"""
#TODO: Handle client connections to the server
:return: VOID
"""
while True:
try:
clienthandler, addr = self.serversocket.accept()
# TODO: from the addr variable, extract the client id assigned to the client
# TODO: send assigned id to the new client. hint: call the send_clientid(..) method
self._handler(clienthandler) # receive, process, send response to client.
except:
# handle exceptions here
pass #remove this line after implemented.
def run(self):
"""
Already implemented for you
Run the server.
:return: VOID
"""
self._listen()
self._accept_clients()
# main execution
if __name__ == '__main__':
server = Server()
server.run()
2)
import threading
class ClientHandler(object):
"""
The client handler class receives and process client requests
and sends responses back to the client linked to this handler.
"""
def __init__(self, server_instance, clienthandler, addr):
"""
Class constructor already implemented for you.
:param server_instance:
:param clienthandler:
:param addr:
"""
self.clientid = addr[1] # the id of the client that owns this handler
self.server_ip = addr[0]
self.server = server_instance
self.clienthandler = clienthandler
def print_lock(self):
"""
TODO: create a new print lock
:return: the lock created
"""
# your code here.
return None # modify the return to return a the lock created
def process_client_data(self):
"""
TODO: receives the data from the client
TODO: prepares the data to be printed in console
TODO: create a print lock
TODO: adquire the print lock
TODO: prints the data in server console
TODO: release the print lock
:return: VOID
"""
pass # remove this line after implemented