#!/usr/bin/python2
import cgi
import string
import sys
import os
import os.path
#-------------
#-------------MIME type statement:
print "Content-type: text/html\n\n"
#-------------Header:
print "
Student results.\n"
print "\n"
print "\n"
#-------------End header.
#-------------Create field storage object:
form = cgi.FieldStorage()
#-------------Javascript redirector for input errors:
redirector = 'Click here to return to login page.\n'
#-------------End Javascript redirector string.
#-------------Form input error handling:
#IF THE SENDING FORM HAS NOT SENT THE CORRECT KEYS THEN THIS SCRIPT WILL NOT BE
#PARSED BEYOND THE FOLLOWING IF STATEMENT:
if not (form.has_key("firstName") and form.has_key("lastName") and form.has_key("pass")):
print "Error
\n"
print "Please fill in your name and password.
\n" + redirector
print "\n"
sys.exit()
#-------------End form input error handling.
#-------------Storing input from form:
first = form["firstName"].value
last = form["lastName"].value
password = form["pass"].value
#-------------End storing input from form.
#-------------Parsing input file(assn2data.csv):
inputFile = file("assn2data.csv", "r")
studentList = inputFile.readlines()
#Incorrect name or password first assumed:
outputString = "Your name or password is not correct.
\n" + redirector
for i in range(len(studentList)):
currentStudent = studentList[i]
if studentList[i].startswith('"'):
fields = studentList[i].split(",")
fileLastName = fields[0].replace('"','')
fileLastName = fileLastName.strip()
fileFirstName = fields[1].replace('"','')
fileFirstName = fileFirstName.strip()
if ((fileLastName==last) and (fileFirstName==first) and (fields[2]==password)):
outputString = ''
fieldNamesDummy = "dummy, " + studentList[0]
fieldNames = fieldNamesDummy.split(",")
for a in range(3,len(fields)):
outputString = outputString + "" + fieldNames[a] + " | " \
+ fields[a] + " |
\n"
outputString = outputString + '
\n
' + \
'Back to login page.\n'
inputFile.close()
#-------------End parsing input file.
#-------------Output:
print ""
print first
print last
print "
"
print outputString
#-------------End output.
#Footer:
print "