Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Client class that provides functionality to create a client socket is provided. Implement all the methods but bind(..) class Client(object): def __init__(self): Class
Client class that provides functionality to create a client socket is provided. Implement all the methods but bind(..) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Client(object): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Class constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.id = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def connect(self, server_ip_address, server_port): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: Create a connection from client to server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Note that this method must handle any exceptions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:server_ip_address: the know ip address of the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:server_port: the port of the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pass # delete this line after implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def bind(self, client_ip='', client_port=12000): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DO NOT IMPLEMENT, ALREADY IMPLEMENTED | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This method is optional and only needed when the order or range of the ports bind is important | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not called, the system will automatically bind this client to a random port. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:client_ip: the client ip to bind, if left to '' then the client will bind to the local ip address of the machine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:client_port: the client port to bind. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.client.bind((client_ip, client_port)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def send(self, data): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: Serializes and then sends data to server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:param data: the raw data to serialize (note that data can be in any format.... string, int, object....) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: VOID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pass # delete this line after implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def receive(self, max_alloc_buffer=4090): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: Deserializes the data received by the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:param max_alloc_buffer: Max allowed allocated memory for this data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: the deserialized data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
deserialized_data = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return deserialized_data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def client_helper(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: create an object of the client helper and start it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pass # delete this line after implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def close(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: close this client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: VOID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pass # delete this line after implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# main code to run client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if __name__ == '__main__': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
server_ip = '127.0.0.1' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
server_port = 12000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client = Client() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client.connect(server_ip, server_port) # creates a connection with the server
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class ClientHelper: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __init__(self, client): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.client = client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
student_name = '' # TODO: your name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
student_id = 0 # TODO: your student id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
github_username = '' # TODO: your github username | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def create_request(self, name, id, github_username): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: create request with a Python dictionary to save the parameters given in this function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
the keys of the dictionary should be 'student_name', 'github_username', and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'sid'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: the request created | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
request = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def send_request(self, request): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: send the request passed as a parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:request: a request representing data deserialized data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pass # remove this line after the method is implemented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def process_response(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: process a response from the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Note the response must be received and deserialized before being processed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:response: the serialized response. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pass # remove this line after the method is implemented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def start(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TODO: create a request with your student info using the self.request(....) method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
send the request to the server, and then process the response sent from the server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass # remove this line after the method is implemented
|
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