#!/usr/bin/env python # # An example that presents CAPTCHA tests in a web environment # and gives the user a chance to solve them. Run it, optionally # specifying a port number on the command line, then point your web # browser at the given URL. # from Captcha.Visual import Tests from Captcha import Factory import BaseHTTPServer, urlparse, sys class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): scheme, host, path, parameters, query, fragment = urlparse.urlparse(self.path) # Split the path into segments pathSegments = path.split('/')[1:] # Split the query into key-value pairs args = {} for pair in query.split("&"): if pair.find("=") >= 0: key, value = pair.split("=", 1) args.setdefault(key, []).append(value) else: args[pair] = [] # A hack so it works with a proxy configured for VHostMonster :) if pathSegments[0] == "vhost": pathSegments = pathSegments[3:] if pathSegments[0] == "": self.handleRootPage(args.get('test', Tests.__all__)[0]) elif pathSegments[0] == "images": self.handleImagePage(pathSegments[1]) elif pathSegments[0] == "solutions": self.handleSolutionPage(pathSegments[1], args['word'][0]) else: self.handle404() def handle404(self): self.send_response(404) self.send_header("Content-Type", "text/html") self.end_headers() self.wfile.write("
%s: %s
Or try...
%s
You guessed: %s
Possible solutions: %s
""" % (test.__class__.__name__, test.id, result, word, ", ".join(test.solutions))) def main(port): print "Starting server at http://localhost:%d/" % port handler = RequestHandler handler.captchaFactory = Factory() BaseHTTPServer.HTTPServer(('', port), RequestHandler).serve_forever() if __name__ == "__main__": # The port number can be specified on the command line, default is 8080 if len(sys.argv) >= 2: port = int(sys.argv[1]) else: port = 8080 main(port) ### The End ###