#!/usr/bin/python2.4 import urllib import sys import cgi from xml.dom.minidom import parse, parseString AsirraServiceUrl = "http://challenge.asirra.com/cgi/Asirra" def ValidateAsirraChallenge(form): # We get a quoted string and keep it quoted in order to construct a query url ticket = form.getfirst("Asirra_Ticket") validationUrl = ("%s?action=ValidateTicket&ticket=%s" % (AsirraServiceUrl, ticket)) validationStream = urllib.urlopen(validationUrl) dom = parse(validationStream) validationStream.close() value = dom.getElementsByTagName("Result")[0].childNodes[0].data # If Asirra tells us the challenge was passed, return without # throwing an exception if (value == "Pass"): return # For the purpose of this example, we print some debug # info. You'll probably want to remove this from your # real implementation, to prevent leaking information # to attackers. DebugMessage = "unknown captcha failure" try: DebugMessage = dom.getElementsByTagName("Debug")[0].childNodes[0].data except: pass raise Exception("Asirra Error: %s" % (DebugMessage)) def main(): sys.stdout.write("Content-Type: text/html\n\n") try: form = cgi.FieldStorage() UserName = form.getfirst("UserName") FavoriteColor = form.getfirst("FavoriteColor") try: ValidateAsirraChallenge(form) except: sys.stdout.write("You're trying to cheat the system, aren't you? " "Bots and cheaters are not allowed at ExampleService!\n") raise sys.stdout.write("
Welcome, new user %s! You are a human! And your favorite color is %s." % (UserName, FavoriteColor)) except Exception, ex: sys.stdout.write("
Got an exception:
%s" % ex) sys.stdout.write("
You can try this again.\n") sys.stdout.write("You can also see the Source Code to ExampleService") main()