ATM Socket Program in Python Code two separate programs a cl
ATM Socket Program in Python:
Code two separate programs: a client and a server. The server balances and does all updates, i.e. deposits and withdrawals. The server will also respond when asked about the account balance. A user sends commands through the client, able to deposit money into their account, withdraw money from the same account, and check the balance of the account. No calculations are done via the client; the client only sends requests to the server, which executes whatever the request is and returns the answer to the client. The balance should be initialized to an X amount by the server.
Solution
class MySocket: \"\"\"demonstration class only - coded for clarity, not efficiency \"\"\" def __init__(self, sock=None): if sock is None: self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) else: self.sock = sock def connect(self, host, port): self.sock.connect((host, port)) def mysend(self, msg): totalsent = 0 while totalsent < MSGLEN: sent = self.sock.send(msg[totalsent:]) if sent == 0: raise RuntimeError(\"socket connection broken\") totalsent = totalsent + sent def myreceive(self): chunks = [] bytes_recd = 0 while bytes_recd < MSGLEN: chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048)) if chunk == b\'\': raise RuntimeError(\"socket connection broken\") chunks.append(chunk) bytes_recd = bytes_recd + len(chunk) return b\'\'.join(chunks)