在GAE中使用reCAPTCHA

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/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-