Sie sind auf Seite 1von 4

1)

# don't modify this imports.


import socket
import pickle
from threading import Thread

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 __init__(self, host="127.0.0.1", port = 12000):


"""
Class constructor
:param host: by default localhost. Note that '0.0.0.0' takes LAN ip address.
:param port: by default 12000
"""
self.host = host
self.port = port
self.serversocket = None # TODO: create the server socket

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 _handler(self, clienthandler):


"""
#TODO: receive, process, send response to the client using this handler.
:param clienthandler:
:return:
"""
while True:
# TODO: receive data from client
# TODO: if no data, break the loop
# TODO: Otherwise, send acknowledge to client. (i.e a message saying 'server got the
data
pass # remove this line after implemented.

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 _send_clientid(self, clienthandler, clientid):


"""
# TODO: send the client id to a client that just connected to the server.
:param clienthandler:
:param clientid:
:return: VOID
"""
pass # remove this line after implemented.

def send(self, clienthandler, data):


"""
# TODO: Serialize the data with pickle.
# TODO: call the send method from the clienthandler to send data
:param clienthandler: the clienthandler created when connection was accepted
:param data: raw data (not serialized yet)
:return: VOID
"""
pass #remove this line after implemented.

def receive(self, clienthandler, MAX_ALLOC_MEM=4096):


"""
# TODO: Deserialized the data from client
:param MAX_ALLOC_MEM: default set to 4096
:return: the deserialized data.
"""
return None #change the return value after implemente.

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

Das könnte Ihnen auch gefallen