Advertisement
Guest User

app.py

a guest
Nov 17th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. from flask import Flask, render_template, request, redirect, url_for, make_response, jsonify
  2. import random, sqlite3, datetime, time
  3.  
  4. app = Flask(__name__)
  5. banned_ip = []
  6.  
  7. file = open('banned_ips.txt', 'r')
  8. print("Banned IPs:")
  9. for line in file:
  10.     print(line)
  11.     banned_ip.append(line)
  12.  
  13. @app.route('/', methods=['GET', 'POST'])
  14. def login():
  15.  
  16.     ### VARIABLES ###
  17.     conn = sqlite3.connect('leakedsarp.db')
  18.     c = conn.cursor()
  19.     #################
  20.  
  21.     if(request.method == 'POST'):
  22.         info = ''
  23.         user_data = []
  24.         username = request.form['username'].lower()
  25.         password = request.form['password']
  26.         ip = str(request.remote_addr)
  27.  
  28.         #c.execute("INSERT INTO logs VALUES({},{},{})".format(ip, username, password))
  29.         conn.commit()
  30.  
  31.         if(len(username) < 1 or len(password) < 1):
  32.             return render_template('index.html', info='Username or password must be greater than one character.')
  33.  
  34.         else: #If input is provided, the below query searches the database and compares the username to others.
  35.             select = c.execute("SELECT * FROM accounts WHERE lower(username)=? LIMIT 1", [username])
  36.             for row in select.fetchall(): #Fetches the rows and imports them into an array.
  37.                 for data in row:
  38.                     user_data.append(data) #Makes sure each entry is a different list item.
  39.  
  40.             if(len(user_data) == 0): # Returns 0 if there is no account.
  41.                 info = 'Username "{}" not found.'.format(username)
  42.                 return render_template('index.html', info=info)
  43.  
  44.             else: # Otherwise an account is found and compares with the data.
  45.                 if(user_data[1].lower() == username and user_data[2] == password):
  46.                     logged_in = True
  47.                     return render_template('index.html', info="Welcome", logged_in=logged_in)
  48.                 else:
  49.                     return render_template('index.html', info="Invalid Password")
  50.     else:
  51.         if(request.remote_addr in banned_ip):
  52.             return "IP address {} is banned.".format(request.remote_addr)
  53.         return render_template('index.html')
  54.  
  55. @app.route("/ip", methods=["GET"])
  56. def get_my_ip():
  57.     return request.remote_addr
  58. if (__name__ == '__main__'):
  59.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement