在GAE中使用reCAPTCHA

Google App Engine是好东东,容易实现一些基于Web的小想法;reCAPTCHA也是好东西,大家都一起来防SPAM。而我就这么一点小乐趣,东搞搞西搞搞。
在GAE上面使用reCAPTCHA也很容易,并且已经有人写出来了具体怎么做,偶也依样画葫芦写了个演示,没有什么实际的功能,纯属瞎折腾看看自己验证码输对了没。

#!/usr/bin/env python
import wsgiref.handlers
 
import os
from os import environ
from recaptcha.client import captcha
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
 
class MainHandler(webapp.RequestHandler):
  def get(self):
	chtml = captcha.displayhtml(
		public_key = "public_key",
		use_ssl = False,
		error = None)
	template_values = {
	'captchahtml': chtml
        }
    #self.response.out.write('Hello world!')
	path = os.path.join(os.path.dirname(__file__), 'index.html')
	self.response.out.write(template.render(path, template_values))
  def post(self):
	challenge = self.request.get('recaptcha_challenge_field')
	response  = self.request.get('recaptcha_response_field')
	remoteip  = environ['REMOTE_ADDR']
	cResponse = captcha.submit(
                    challenge,
                    response,
                    "PRIVATE-KEY",
                    remoteip)
	if cResponse.is_valid:
          # response was valid
          # other stuff goes here
          template_values = {
            'hello': "Hello world!"
            }
        else:
          error = cResponse.error_code
          chtml = captcha.displayhtml(
			public_key = "public_key",
			use_ssl = False, 
			error = cResponse.error_code)
          template_values = {
            'captchahtml': chtml
            }
 
	path = os.path.join(os.path.dirname(__file__), 'index.html')
	self.response.out.write(template.render(path, template_values))
 
 
def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)
 
 
if __name__ == '__main__':
  main()

-EOF-