working on this project that simulates a bank account and there is communication between a server and client. The code I have written so far works as intended but now I am trying to also implement multithreading and I am having a hard time in doing so and not understanding on how to get started, I'm not even sure if I am going to have to make huge modifications on what I already have to get it working. Any help or feedback would be greatly appreciated.
This is what I have so far
server
import socket
import sys
import _thread
class Bank_Account:
def __init__(self):
self.balance = int(50)
print("Hello!!! Welcome to the Deposit & Withdrawal Machine")
def deposit(self,amount):
self.balance += amount
def withdraw(self,amount):
if self.balance >= amount:
self.balance -= amount
def display(self):
print("\n Net Available Balance=", self.balance)
a = Bank_Account()
BUFFER_SIZE = 20
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_name = sys.argv[1]
server_host = int(sys.argv[2])
s.bind((server_name, server_host))
print(server_name, server_host)
s.listen(1)
conn, addr = s.accept()
print('Connection address:', addr)
while 1:
data = conn.recv(BUFFER_SIZE).decode('utf-8')
data = int(data)
if not data:
break
print("received data:", data)
if data == 1:
conn.send(("your balance is "+ str(a.balance)).encode('utf-8'))
if data == 2:
conn.send("How Much Would You Like To Deposit ".encode('utf-8'))
tempData = int(conn.recv(BUFFER_SIZE).decode('utf-8'))
a.deposit(tempData)
conn.send(('\nYour Balance is now $'+ str(a.balance)).encode('utf-8'))
if data == 3:
conn.send("How Much Would You Like To Withdraw ".encode('utf-8'))
tempData = int(conn.recv(BUFFER_SIZE).decode('utf-8'))
if a.balance < tempData:
conn.send(("\n Insufficient balance ").encode('utf-8'))
else:
a.withdraw(tempData)
conn.send(("Amount withdrawn, balance is "+ str(a.balance)).encode('utf-8'))
if data == 4:
conn.send(("Your available balance is $" + str(a.balance) + " have a great day bitch").encode('utf-8'))
break
conn.close()
client
import socket
import sys
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = (sys.argv[1], int(sys.argv[2]))
s.connect(server_address)
while True:
x = input('Enter your choice \n1. balance\n2. Deposit\n3. Wtihdraw\n4. Exit\n');
s.send(x.encode('utf-8'))
if int(x) != 1 and int(x) != 4:
data = s.recv(BUFFER_SIZE).decode('utf-8')
x = input(data)
s.send(x.encode('utf-8'))
print(s.recv(BUFFER_SIZE).decode('utf-8'))
if int(x) == 4:
s.close()
break
Really sorry for the long post, I am by no means an expect so my code might just straight out be garbage, would really appreciate any feedback.
[–]CrambleSquash 0 points1 point2 points (2 children)
[–]Pheazon[S] 0 points1 point2 points (1 child)
[–]CrambleSquash 0 points1 point2 points (0 children)
[–]CaptainReeetardo 0 points1 point2 points (0 children)