Skip to content

Updated the lab server, now it is compatible with python3 (tested on Python 3.12.2) #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 66 additions & 95 deletions AndroLabServer/app.py
Original file line number Diff line number Diff line change
@@ -1,158 +1,129 @@
import getopt
import web
import sys
#from web.wsgiserver import CherryPyWSGIServer
#from cherrypy import wsgiserver
from cheroot import wsgi # This replaces the 2 above
from flask import Flask, request, request_started
from functools import wraps
from cheroot import wsgi
from flask import Flask, request
from models import User, Account
from database import db_session
import simplejson as json
makejson = json.dumps
import socket

hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)

print("Hostname:", hostname)
print("IP Address:", ip_address)

app = Flask(__name__)
makejson = json.dumps

DEFAULT_PORT_NO = 8888

def usageguide():
print "InsecureBankv2 Backend-Server"
print "Options: "
print " --port p serve on port p (default 8888)"
print " --help print this message"
print("InsecureBankv2 Backend-Server")
print("Options: ")
print(" --port p serve on port p (default 8888)")
print(" --help print this message")

@app.errorhandler(500)
def internal_servererror(error):
print " [!]", error
print("[!]", error)
return "Internal Server Error", 500

'''
The function handles the authentication mechanism
'''
@app.route('/login', methods=['POST'])
def login():
Responsemsg="fail"
Responsemsg = "fail"
user = request.form['username']
#checks for presence of user in the database #requires models.py
u = User.query.filter(User.username == request.form["username"]).first()
print "u=",u
if u and u.password == request.form["password"]:
Responsemsg="Correct Credentials"
Responsemsg = "Correct Credentials"
elif u and u.password != request.form["password"]:
Responsemsg="Wrong Password"
Responsemsg = "Wrong Password"
elif not u:
Responsemsg="User Does not Exist"
else: Responsemsg="Some Error"
data = {"message" : Responsemsg, "user": user}
print makejson(data)
Responsemsg = "User Does not Exist"
else:
Responsemsg = "Some Error"
data = {"message": Responsemsg, "user": user}
print(makejson(data))
return makejson(data)

'''
The function responds back with the from and to debit accounts corresponding to logged in user
'''
@app.route('/getaccounts', methods=['POST'])
def getaccounts():
#set accounts from the request
Responsemsg="fail"
acc1=acc2=from_acc=to_acc=0
user=request.form['username']
#checks for presence of user in the database
Responsemsg = "fail"
acc1 = acc2 = from_acc = to_acc = 0
user = request.form['username']
u = User.query.filter(User.username == user).first()
if not u or u.password != request.form["password"]:
Responsemsg="Wrong Credentials so trx fail"
Responsemsg = "Wrong Credentials so trx fail"
else:
Responsemsg="Correct Credentials so get accounts will continue"
a=Account.query.filter(Account.user == user)
Responsemsg = "Correct Credentials so get accounts will continue"
a = Account.query.filter(Account.user == user)
for i in a:
if (i.type=='from'):
from_acc=i.account_number;
for j in a:
if (i.type=='to'):
to_acc=i.account_number;
data = {"message" : Responsemsg, "from": from_acc,"to": to_acc}
print makejson(data)
if i.type == 'from':
from_acc = i.account_number
elif i.type == 'to':
to_acc = i.account_number
data = {"message": Responsemsg, "from": from_acc, "to": to_acc}
print(makejson(data))
return makejson(data)

'''
The function takes a new password as input and passes it on to the change password module
'''
@app.route('/changepassword', methods=['POST'])
def changepassword():
#set accounts from the request
Responsemsg="fail"
newpassword=request.form['newpassword']
user=request.form['username']
print newpassword
u = User.query.filter(User.username == user).first() #checks for presence of user in the database
Responsemsg = "fail"
newpassword = request.form['newpassword']
user = request.form['username']
print(newpassword)
u = User.query.filter(User.username == user).first()
if not u:
Responsemsg="Error"
Responsemsg = "Error"
else:
Responsemsg="Change Password Successful"
u.password = newpassword
Responsemsg = "Change Password Successful"
u.password = newpassword
db_session.commit()
data = {"message" : Responsemsg}
print makejson(data)
data = {"message": Responsemsg}
print(makejson(data))
return makejson(data)

'''
The function handles the transaction module
'''

@app.route('/dotransfer', methods=['POST'])
def dotransfer():
#set accounts from the request
Responsemsg="fail"
user=request.form['username']
amount=request.form['amount']
#print request.form["from_acc"]
u = User.query.filter(User.username == user).first() #checks for presence of user in the database
Responsemsg = "fail"
user = request.form['username']
amount = request.form['amount']
u = User.query.filter(User.username == user).first()
if not u or u.password != request.form["password"]:
Responsemsg="Wrong Credentials so trx fail"
#print Responsemsg
Responsemsg = "Wrong Credentials so trx fail"
else:
Responsemsg="Success"
#print Responsemsg
from_acc = request.form["from_acc"]
to_acc = request.form["to_acc"]
amount = request.form["amount"]
Responsemsg = "Success"
from_acc = request.form["from_acc"]
to_acc = request.form["to_acc"]
amount = request.form["amount"]
from_account = Account.query.filter(Account.account_number == from_acc).first()
to_account = Account.query.filter(Account.account_number == to_acc).first()
#print "fromacc=",from_account
#print "amount===",amount
to_account.balance += int(request.form['amount'])
from_account.balance -= int(request.form['amount'])
db_session.commit()
data = {"message" : Responsemsg, "from": from_acc, "to": to_acc, "amount": amount}
#print makejson(data)
data = {"message": Responsemsg, "from": from_acc, "to": to_acc, "amount": amount}
return makejson(data)

'''
The function provides login mechanism to a developer user during development phase
'''
@app.route('/devlogin', methods=['POST'])
def devlogin():
user=request.form['username']
Responsemsg="Correct Credentials"
data = {"message" : Responsemsg, "user": user}
print makejson(data)
user = request.form['username']
Responsemsg = "Correct Credentials"
data = {"message": Responsemsg, "user": user}
print(makejson(data))
return makejson(data)

if __name__ == '__main__':
port = DEFAULT_PORT_NO
options, args = getopt.getopt(sys.argv[1:], "", ["help", "port="])
for op, arg1 in options:
if op == "--help":
for arg in sys.argv[1:]:
if arg == "--help":
usageguide()
sys.exit(2)
elif op == "--port":
port = int(arg1)
elif arg.startswith("--port="):
port = int(arg.split('=')[1])

urls = ("/.*", "app")
apps = web.application(urls, globals())
server = wsgi.Server(("0.0.0.0", port),app,server_name='localhost')
print "The server is hosted on port:",(port)
server = wsgi.Server(("0.0.0.0", port), app, server_name='localhost')
print("The server is hosted on port:", port)

try:
server.start()
#apps.run(port)
except KeyboardInterrupt:
server.stop()
8 changes: 3 additions & 5 deletions AndroLabServer/database.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine('sqlite:///mydb.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
engine = create_engine('sqlite:///mydb.db')
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()

Expand Down