def main(): request = Request() toDo = request.data["button"].value name = request.data["name"].value password = request.data["password"].value br = "<br />" title = "" body = "" formBody = """ <button type="submit" name="button" value="yes">はい</button> <button type="submit" name="button" value="no">いいえ</button> """ form = None if toDo == "reset": title = "Reset score" body = "スコアをリセットします。よろしいですか?" + br form = Form("post", "/cgi-bin/delete.py", formBody, ("name", name), ("password", password), ("toDo", "reset")) elif toDo == "delete": title = "Delete account" body = "アカウントを消去します。よろしいですか?" + br form = Form("post", "/cgi-bin/delete.py", formBody, ("name", name), ("password", password), ("toDo", "delete")) body += str(form) + br res = Response(title, body) res.respond()
def main(): br = "<br />" request = Request() name = request.data["name"].value number = int(request.data["number"].value) score = int(request.data["score"].value) if number != 1: answer = request.data["answer"].value rightAnswer = request.data["rightAnswer"].value if rightAnswer == answer: score = score + 1 title = "Question No.{number}".format(number=str(number)) if number == 2: link = "/cgi-bin/gameOver.py" else: link = "/cgi-bin/question.py" question = IntQuestion() hidden = [("name", name), ("number", str(number + 1)), ("score", str(score)), ("rightAnswer", str(question.answer()))] formBody = """ {question} = <input type="text" name="answer" />{br} <button type="submit">確定</button>{br} """.format(question=str(question), br=br) form = Form("post", link, formBody, *hidden) body = str(form) res = Response(title, body) res.respond()
def main(): br = "</br>" title = "Python練習1" body = "Hello, Python!" + br version = list(sys.version_info)[0:3] body += "version: {0[0]}.{0[1]}.{0[2]}".format(version) body += br response = Response(title, body) response.respond()
def main(): try: dbConnector = MySQLdb.connect( user = dbName, passwd = dbPass, host = "localhost", db = "mathQuizUsers" ) dbCursor = dbConnector.cursor() request = Request() data = request.data title = "Sign up success!" br = "<br />" body = "" errorLoc = "Location: /cgi-bin/signUp.py?error={error}\n" if "name" not in data: print(errorLoc.format(error = "noName")) elif "password" not in data: print(errorLoc.format(error = "noPass")) else: name = data["name"].value password = data["password"].value dbCursor.execute('SELECT * FROM users WHERE name = "{name}"'.format(name = name)) if dbCursor.fetchall() != (): print(errorLoc.format(error = "alreadyExist")) else: dbCursor.execute(""" INSERT INTO users VALUES ("{name}", "{password}", 0) """.format(name = name, password = password)) dbConnector.commit() dbData = DBExpression("Name", "Score") dbCursor.execute("SELECT * FROM users") for row in dbCursor.fetchall(): dbData.appendData(row[0], str(row[2])) body += """ <form method="post" action="/cgi-bin/question.py"> <button type="submit">Start!</button> <input type="hidden" name="name" value="{name}" /> <input type="hidden" name="number" value="1" /> <input type="hidden" name="score" value="0" /> </form> """.format(name = name) body += str(dbData) + br res = Response(title, body) res.respond() finally: dbCursor.close() dbConnector.close()
def main(): br = "<br />" title = "Math Quiz" body = """ <button type="button" onclick="location.href='/cgi-bin/signUp.py?error=none'"> Sign up </button> <button type="button" onclick="location.href='/cgi-bin/signIn.py?error=none'"> Sign in </button> """ res = Response(title, body) res.respond()
def main(): br = "<br />" title = "Game Over" try: dbConnector = MySQLdb.connect(user=dbName, passwd=dbPass, host="localhost", db="mathQuizUsers") dbCursor = dbConnector.cursor() request = Request() score = int(request.data["score"].value) answer = request.data["answer"].value rightAnswer = request.data["rightAnswer"].value if rightAnswer == answer: score = score + 1 name = request.data["name"].value dbCursor.execute(""" SELECT * FROM users WHERE name = "{name}" """.format(name=name)) if score > dbCursor.fetchone()[2]: dbCursor.execute(""" UPDATE users SET score = {score} WHERE name = "{name}" """.format(name=name, score=str(score))) dbConnector.commit() dbData = DBExpression("Name", "Score") dbCursor.execute("SELECT * FROM users") for row in dbCursor.fetchall(): dbData.appendData(row[0], str(row[2])) body = """ score = {score}{br} """.format(br=br, score=str(score)) body += str(dbData) + br res = Response(title, body) res.respond() finally: dbCursor.close() dbConnector.close()
def main(): br = "<br />" title = "Sign In" request = Request() error = request.data["error"].value errorMessage = "" if error == "noName": errorMessage += '<div class="error">Input your name.</div>' + br elif error == "noPass": errorMessage += '<div class="error">Input your password.</div>' + br elif error == "doNotExist": errorMessage += '<div class="error">That name does not exist.</div>' + br elif error == "wrongPass": errorMessage += '<div class="error">That password is wrong.</div>' + br body = """ {errorMessage} <form method="post" action="/cgi-bin/signInSuccess.py"> Name: <input type="text" name="name" />{br} Password: <input type="password" name="password" />{br} <button type="submit" name="submit" value="signIn">Sign in</button>{br} </form> """.format(errorMessage = errorMessage, br = br) res = Response(title, body) res.respond()
def main(): try: dbConnector = MySQLdb.connect(user=dbName, passwd=dbPass, host="localhost", db="mathQuizUsers") dbCursor = dbConnector.cursor() request = Request() data = request.data title = "Sign in success!" br = "<br />" body = "" errorLoc = "Location: /cgi-bin/signIn.py?error={error}\n" if "name" not in data: print(errorLoc.format(error="noName")) elif "password" not in data: print(errorLoc.format(error="noPass")) else: name = data["name"].value password = data["password"].value dbCursor.execute( 'SELECT * FROM users WHERE name = "{name}"'.format(name=name)) users = dbCursor.fetchall() if users == (): print(errorLoc.format(error="doNotExist")) elif len(users) >= 2: raise DBError() else: if users[0][1] != password: print(errorLoc.format(error="wrongPass")) else: dbData = DBExpression("Name", "Score") dbCursor.execute("SELECT * FROM users") for row in dbCursor.fetchall(): dbData.appendData(row[0], str(row[2])) hidden = [("name", name), ("number", "1"), ("score", "0")] formBody = """ <button type="submit">Start!</button> """ form = Form("post", "/cgi-bin/question.py", formBody, *hidden) deleteFormBody = """ <button class="red" type="submit" name="button" value="delete"> Delete account </button> <button class="red" type="submit" name="button" value="reset"> Reset score </button> """ deleteForm = Form("post", "/cgi-bin/deleteConfirm.py", deleteFormBody, ("name", name), ("password", password)) body += str(form) + br body += str(deleteForm) + br body += str(dbData) + br res = Response(title, body) res.respond() finally: dbCursor.close() dbConnector.close()