如何使用Python插件进行reCaptcha验证?
我想做一个验证码验证。
我从reCAPTCHA网站获取了密钥,并且已经成功将公钥放到网页上以加载挑战。
<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=<your_public_key>">
</script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=<your_public_key>"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
我下载了reCAPTCHA的Python插件,但是我找不到任何关于如何使用它的文档。
有没有人知道怎么使用这个Python插件?recaptcha-client-1.0.4.tar.gz (md5)
2 个回答
4
很抱歉告诉你,这个模块虽然能正常工作,但几乎没有任何文档说明,对于那些喜欢在安装后使用“>> help(modulename)”来获取帮助的人来说,它的布局有点让人困惑。我会用cherrypy举个例子,之后再说一些和cgi相关的内容。
captcha.py包含两个函数和一个类:
display_html:这个函数会返回我们熟悉的“reCaptcha框”。
submit:这个函数会在后台提交用户输入的值。
RecapchaResponse:这是一个容器类,用来存放来自reCaptcha的响应。
你首先需要导入captcha.py的完整路径,然后创建几个函数来处理显示和处理响应。
from recaptcha.client import captcha
class Main(object):
@cherrypy.expose
def display_recaptcha(self, *args, **kwargs):
public = "public_key_string_you_got_from_recaptcha"
captcha_html = captcha.displayhtml(
public,
use_ssl=False,
error="Something broke!")
# You'll probably want to add error message handling here if you
# have been redirected from a failed attempt
return """
<form action="validate">
%s
<input type=submit value="Submit Captcha Text" \>
</form>
"""%captcha_html
# send the recaptcha fields for validation
@cherrypy.expose
def validate(self, *args, **kwargs):
# these should be here, in the real world, you'd display a nice error
# then redirect the user to something useful
if not "recaptcha_challenge_field" in kwargs:
return "no recaptcha_challenge_field"
if not "recaptcha_response_field" in kwargs:
return "no recaptcha_response_field"
recaptcha_challenge_field = kwargs["recaptcha_challenge_field"]
recaptcha_response_field = kwargs["recaptcha_response_field"]
# response is just the RecaptchaResponse container class. You'll need
# to check is_valid and error_code
response = captcha.submit(
recaptcha_challenge_field,
recaptcha_response_field,
"private_key_string_you_got_from_recaptcha",
cherrypy.request.headers["Remote-Addr"],)
if response.is_valid:
#redirect to where ever we want to go on success
raise cherrypy.HTTPRedirect("success_page")
if response.error_code:
# this tacks on the error to the redirect, so you can let the
# user knowwhy their submission failed (not handled above,
# but you are smart :-) )
raise cherrypy.HTTPRedirect(
"display_recaptcha?error=%s"%response.error_code)
如果使用cgi,基本上也是一样的,只需在我使用cherrypy的request.headers的地方用REMOTE_ADDR环境变量,并使用字段存储来进行检查。
这里没有什么魔法,这个模块只是按照文档来操作的: https://developers.google.com/recaptcha/docs/display
你可能需要处理的验证错误: https://developers.google.com/recaptcha/docs/verify
25
这件事其实很简单。这里有一个我正在使用的简单跟踪插件的例子:
from recaptcha.client import captcha
if req.method == 'POST':
response = captcha.submit(
req.args['recaptcha_challenge_field'],
req.args['recaptcha_response_field'],
self.private_key,
req.remote_addr,
)
if not response.is_valid:
say_captcha_is_invalid()
else:
do_something_useful()
else:
data['recaptcha_javascript'] = captcha.displayhtml(self.public_key)
data['recaptcha_theme'] = self.theme
return 'recaptchaticket.html', data, n