#!/usr/bin/python3 """Defines the read and write methods used by network to send/receive""" import socket, sys, pickle class MySocket: def read(self, socket): '''Parameters: socket, a socket returned by an accept command. Preconditions: socket has been accepted and is open. Socket is sending a pickled object. Postconditions: It will return the object in socket in an unpickled format.''' """Given a socket, receives a pickled object from it and loads it.""" response = socket.recv(64) st = response while(len(st) > 0 and st[-1] != 42 ): response = socket.recv(64) st += response if st == None or len(st) == 0: return None else: return pickle.loads(st[:-1]) def write(self, socket, obj): ''' Parameters: socket, a socket returned by an accept command obj, a pickleable object to be sent. Preconditions: socket has been accepted and is open Postconditions: obj will be sent to the destination marked by socket as a pickled object.''' """Given a socket and obj, pickles and sends obj through socket.""" p = pickle.dumps(obj) p += b'*' socket.send(p) def main(): """Runs until some quit condition""" while True: write(client, input("Enter some text: ")) print(read(client))