16

我想做一个验证码验证。

我从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)

4

2 回答 2

25

这很简单。这是我正在使用的一个微不足道的 trac 插件的示例:

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
于 2009-09-17T18:06:28.607 回答
4

很抱歉,这个模块虽然工作得很好,但几乎完全没有文档,而且它的布局对于我们这些喜欢在安装后使用“>> help(modulename)”的人来说有点混乱。我将给出一个使用cherrypy的例子,然后做一些cgi相关的评论。

captcha.py 包含两个函数和一个类:

  • display_html:返回熟悉的“reCaptcha 框”

  • submit:提交用户在后台输入的值

  • RecapchaResponse:这是一个容器类,包含来自 reCaptcha 的响应

您首先需要导入 capcha.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

于 2012-08-20T21:24:30.870 回答