def login(): print('Content-Type: text/html') print() #question 1 #print(json.dumps(dict(os.environ),indent=2)) #print() #question 2 #The Query_String parameter when obtaining hello.py #print("parameters: ") #print(os.environ["Query_String"]) # print(''' # <!doctype html> # <html> # <body> # <ul> # ''') # for item in os.environ["QUERY_STRING"].split('%'): # print(f"<li>"+str(item)+"</li>") # #print("<li>hello</li>") # print(''' # </ul> # </body> # </html> # ''') #question 3 # "HTTP_USER_AGENT" displays the OS and browser information #question3.1 print(templates.login_page())
def serve_html_info(): # Read POST data if we got any. Thank you Hazel! ╰(◕ ᗜ ◕ )╯ posted_bytes = os.environ.get("CONTENT_LENGTH", 0) posted = None if posted_bytes: posted = sys.stdin.read(int(posted_bytes)) # cardboard-thin authentication! if secret.username in posted and secret.password in posted: # set a cookie print('Set-Cookie: auth='+str(True)) print('Content-Type: text/html') print() print(""" <!doctype html> <html> <body> <h1>Lab 3</h1>""") # Display browser info print(f"<p>HTTP_USER_AGENT: {os.environ['HTTP_USER_AGENT']}</p>") # Display query string info # example query: ?name="hugh"&second="two" print(f"<p>QUERY_STRING: {os.environ['QUERY_STRING']}</p>") query = os.environ['QUERY_STRING'] if '&' in query and '=' in query: print("<ul>") for parameter in os.environ['QUERY_STRING'].split('&'): (name, value) = parameter.split('=') print(f"<li><em>{name}</em> = {value}</li>") print(f"</ul>") # Display the POST from the form if posted: print(f"<p>POSTED: <pre>") for line in posted.splitlines(): print(line) print("</pre></p>") # Display a login form OR a secret page if we're logged in if os.environ['HTTP_COOKIE']: # NOTE: only works for one cookie (cookie, value) = os.environ['HTTP_COOKIE'].split('=') if cookie=="auth" and value=="True": # probably incorrect (should pass from `posted` instead?) print(templates.secret_page(secret.username, secret.password)) else: print("<p>Cookie is incorrect somehow</p>") else: this_script = __file__.split('/')[-1:][0] # E.Big stackoverflow.com/a/46390072 print(templates.login_page(this_script)) # pass in this script's name print(""" </body> </html> """)
password = s.getfirst('password') #print(username, password) form_ok = username == secret.username and password == secret.password c = SimpleCookie(os.environ['HTTP_COOKIE']) c_username = None c_password = None if c.get("username"): c_username = c.get('username').value if c.get("password"): c_password = c.get('password').value cookie_ok = c_username == secret.username and c_password == secret.password if cookie_ok: username = c_username password = c_password if form_ok: print("Set-Cookie: username="******"set-Cookie: password=", password) print() if not username and not password: print(login_page()) elif username == secret.username and password == secret.password: print(secret_page(username, password)) else: print(after_login_incorrect())
import os import json import templates import sys import secret # print('Content-Type: application/json') # print() # print(json.dumps(dict(os.environ), indent=2)) # cookies set up stolen from: https://www.tutorialspoint.com/How-to-setup-cookies-in-Python-CGI-Programming print(f"Set-Cookie:username = {secret.username}") print(f"Set-Cookie:password = {secret.password}") print('Content-Type: text/html') print() print(templates.login_page()) posted_bytes = os.environ.get("CONTENT_LENGTH", 0) if posted_bytes: posted = sys.stdin.read(int(posted_bytes)) print(f"<p> POSTED: <pre>") for line in posted.splitlines(): print(line) print("</pre></p>") # print(''' # <!doctyoe html> # <htmml> # <body> # <h1>Hello world</h1> # <ul>
username = cookie.split('=')[1] elif "password" in cookie: password = cookie.split('=')[1] if username and password: print(templates.secret_page(username, password)) elif os.environ.get("REQUEST_METHOD", "GET") == "POST": # no cookies was set form = cgi.FieldStorage() if "username" not in form or "password" not in form: print("<H2>Error</H2>") print("Please fill in the name and addr fields.") else: username = form["username"].value password = form["password"].value print("<p>") print("username:"******"username"].value) print("password:"******"password"].value) print("</p>") if username == secret.username and password == secret.password: # correct login, set cookie print("Set-Cookie: username={};".format(username)) print("Set-Cookie: password={};".format(password)) print(templates.secret_page(username, password)) else: print(templates.after_login_incorrect()) else: print(templates.login_page(), end="\n\n")
username = username.split("=")[1] password = password.split("=")[1] if username == cookieUsername and password == cookiePassword: html += templates.secret_page(username, password) html += """ </body> </html> """ #if there are no cookies then show login page else: # Code for getting POST data taken from lab 3 tips posted_bytes = os.environ.get("CONTENT_LENGTH", 0) if posted_bytes: posted = sys.stdin.read(int(posted_bytes)) html += ("<p> POSTED: <pre>") for line in posted.splitlines(): html += line html += "</pre></p>" username, password = line.split("&") username = username.split("=")[1] password = password.split("=")[1] if username == secret.username and password == secret.password: print("Set-Cookie: authenticated=" + username + "&" + password) html += templates.login_page() html += """ </body> </html> """ print(html)