Flask - Simple Login

Flask References:



Screens:

/ page
/update.html page

Notes:


Code:

# w8_v02.py

# from ip2geotools.databases.noncommercial import DbIpCity
# response = DbIpCity.get('147.229.2.90', api_key='free')
# print (response)

def s01 (st):
   return  """
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <html>
      <head>
        
         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
         <title>""" + st + """</title></head><body>"""
        
sib = """
         <h1>Login type POST:</h1>
         <ul>
         <li>form: action=login method=post
         <li>label: User Name:
         <li>input: type=text name=un
         <li>label: Password:
         <li>input: type=password name=pwd
         <li>input: type="submit" class=submit value="log in"
         </ul>
         <form action=/ method=post>
            <label>User Name:</label>
            <input type=text name=un>
            <br>
            <label>Password:</label>
            <input type=password name=pwd>
            <br>
            <input type="submit" value="log in">
         </form>
"""

s99 = """</body></html>"""

sic = """
         <h1>Login type POST:</h1>
         <form action=update.html method=post>
            <label>New Password:</label>
            <input type=password name=pwda>
            <br>
            <label>Confirm New Password:</label>
            <input type=password name=pwdb>
            <br>
            <input type="submit" value="Accept">
            <input type="submit" value="Cancel" formaction=/>
         </form>
"""

users = {"admin":"123", "nick":"abc"}
ip = "1.1.1.1"
a = "a"
b = "b"
st = "empty"

from flask import Flask
from flask import request, redirect

app = Flask(__name__)

# @app.route("/index.html", methods=['GET', 'POST'])
# @app.route('/login', methods=['GET', 'POST'])
@app.route('/', methods=['GET', 'POST'])

def helloIndex():
   global a, b, ip, st
   pg = s01("version 01") + sib + s99
   ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
   st = "ip: " + ip + " - request: " + str(request)
   print (st)
   a = request.form.get ("un")
   b = request.form.get ("pwd")
   if a != None and users.get(a) == b:
      pg = redirect ("/update.html")
   return pg

# do both get & post to get POST values
# default is just get values
@app.route('/update.html', methods=['GET', 'POST'])

# http://10.0.0.66:8080/update.html
def update():
   if request.method == 'POST':
      w1 = request.form
      u   = str(w1.get("pwda"))
      p   = str(w1.get("pwdb"))
      print ("---- URL: " + request.url + " ---- Posting: " + str(w1))
      return redirect ("/")
   print ("Getting")
   s2  = str(s01 ("w8_02.py, version 02"))
   s2 += "st: " + st + "<p>"
   s2 += sic
   s2 += s99
   return s2

# standard run host server command:  
# app.run(host='0.0.0.0', port= 8080)
# More reliable:
if __name__ == "__main__":
    app.run(host='0.0.0.0', port= 8080)


Port

If you get a "OSError: [Errno 48] Address already in use" kind of error, (on port 8080) try:

(end)