Login form : form « CGI Web « Python Tutorial






<HTML>
<HEAD><TITLE>Login Page</TITLE></HEAD>
<BODY>
<CENTER>
<FORM method="POST" action="http://yourserver/cgi-bin/login.py">
<paragraph> Enter your login name: <input type="text" name="login">
<paragraph> Enter your password: <input type=password name="password">
<paragraph> <input type="submit" value="Connect">
</FORM>
</CENTER>
<HR>

</form>
</BODY>
</HTML>


File: login.py

#!/usr/local/bin/python
import cgi

def header(title):
    print "Content-type: text/html\n"
    print "<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n</HEAD>\n<BODY>\n" % (title)

def footer():
    print "</BODY></HTML>"

form = cgi.FieldStorage()
password = "python"

if not form:
    header("Login Response")
elif form.has_key("login") and form["login"].value != "" and form.has_key("password") and form["password"].value == password:
    header("Connected ...")
    print "<center><hr><H3>Welcome back," , form["login"].value, ".</H3><hr></center>"
    print r"""<form><input type="hidden" name="session" value="%s"></form>""" % (form["login"].value)
    print "<H3><a href=browse.html>Click here to start browsing</a></H3>"

else:
    header("No success!")
    print "<H3>Please go back and enter a valid login.</H3>"

footer()








22.3.form
22.3.1.A Simple Form
22.3.2.Creating Self-Posting CGI Scripts
22.3.3.Static Form Web Page (friends.htm)
22.3.4.Demonstrates get method with an XHTML form
22.3.5.Demonstrates post method with an XHTML form
22.3.6.Demonstrates use of cgi.FieldStorage with an XHTML form
22.3.7.List form data
22.3.8.Login form
22.3.9.Receiving Data from an HTML File