Question
I need help with this Python program which handles client connections. So far i have this and im leaving area with mark TODO so you
I need help with this Python program which handles client connections. So far i have this and im leaving area with mark TODO so you can do your work with explaination of what you did.
class ClientHelper:
def create_request(self, name, id, 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', '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.
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.
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.
Class client:
import socket
import pickle
from clienthelper import ClientHelper
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:"""
def bind(self, client_ip='', client_port=12000):
""" DO NOT IMPLEMENT, ALREADY IMPLEMENTED. This method is optional """
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"""
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."""
def close(self):
""" TODO: close this client:return: VOID """
# 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 server: DO NOT EDIT THIS
import socket
import pickle
HOST = '127.0.0.1' (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']
username = data['username']
sid = data['sid']
log = "Connected: Student: " + student_name + ", Username: " + sername + ", 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
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