Answered step by step
Verified Expert Solution
Link Copied!

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

# file: server.py
# handles client connections
# Please do not modify this file in this lab. You will create a customized server in lab 3.
# To run this script, open a command line terminal and type: python3 server.py
import socket
import pickle
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 12000 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print("Listening at " + HOST + "/" + str(PORT))
conn, addr = s.accept() # accepts new clients
with conn: # for each client that connects
# addr[1] contains the client id assigned to the client that just connected
client_id = {'clientid': addr[1]}
serialized_data = pickle.dumps(client_id) # creates a stream of bytes
conn.send(serialized_data)
while True:
raw_data = conn.recv(1024) # receives data from this client
if not raw_data:
break
data = pickle.loads(raw_data) # deserializes the data from the client
student_name = data['student_name']
github_username = data['github_username']
sid = data['sid']
log = "Connected: Student: " + student_name + ", Github Username: " + github_username + ", sid: " + str(sid)
print(log)
serialized_data = pickle.dumps(1) # creates a stream of bytes
conn.send(serialized_data) # send acknowledge to client (data received)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Lectures On Public Economics

Authors: Anthony B. Atkinson, Joseph E. Stiglitz

1st Edition

0691166412, 978-0691166414

Students also viewed these Databases questions

Question

What impact is this having on you?

Answered: 1 week ago