import cgi import webapp2 import re form2 = """ <!DOCTYPE html> <html> <head> <title>Sign Up</title> <style type="text/css"> .label {text-align: right} .error {color: red} </style> </head> <body> <h2>Signup</h2> <form method="post"> <table> <tr> <td class="label"> Username </td> <td> <input type="text" name="username" value="%(uname)s"> </td> <td class="error"> %(user_error)s </td> </tr> <tr> <td class="label"> Password </td> <td> <input type="password" name="password" value=""> </td> <td class="error"> %(password_error)s </td> </tr> <tr> <td class="label"> Verify Password </td> <td> <input type="password" name="verify" value=""> </td> <td class="error"> %(verify_error)s </td> </tr> <tr> <td class="label"> Email (optional) </td> <td> <input type="text" name="email" value="%(user_email)s"> </td> <td class="error"> %(email_error)s </td> </tr> </table> <input type="submit"> </form> </body> </html> """ def escape_html(s): return cgi.escape(s, quote = True) USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$") PASS_RE = re.compile("^.{3,20}$") EMAIL_RE = re.compile("^[\S]+@[\S]+\.[\S]+$") def valid_username(username): return USER_RE.match(username) def valid_password(password1, password2): return PASS_RE.match(password1) def valid_verify(password1, password2): if password1 == password2 and PASS_RE.match(password2): return True return False def valid_email(email): if EMAIL_RE.match(email) or email == "": return True return False class VerifyHandler(webapp2.RequestHandler): def writeform(self, uname="", user_error="", password_error="", verify_error="", user_email="", email_error=""): self.response.out.write(form2 % {'uname': uname, 'user_error': user_error, 'password_error': password_error, 'verify_error': verify_error, 'user_email': user_email, 'email_error': email_error}) def get(self): self.writeform() def post(self): user_report="" password_report="" verify_report="" email_report="" username = self.request.get('username') password = self.request.get('password') verify = self.request.get('verify') email = self.request.get('email') if valid_username(username) and valid_password(password, verify) and valid_verify(password, verify) and valid_email(email): self.redirect('/unit2/accepted?username=' + username) else: if not valid_username(username): user_report="That is not a valid username" if not valid_password(password, verify): password_report="That is not a valid password" else: if not valid_verify(password, verify): verify_report="Your passwords didn't match" if not valid_email(email): email_report="That's not a valid email" self.writeform(username,user_report, password_report, verify_report, email, email_report) class AcceptedHandler(webapp2.RequestHandler): #username = os.environment('QUERY_STRING') def get(self): username = self.request.get('username') self.response.out.write('Welcome, ' + username) app = webapp2.WSGIApplication([('/unit2/verify', VerifyHandler), ('/unit2/accepted', AcceptedHandler)], debug=True)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question