如何使用Python插件reCaptcha客户端进行验证?

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)

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

嗨,Abbot, 我是 Python 的新手,请问你能详细解释一下如何使用下载的软件包吗? - Hoang Pham
2
你应该将它安装为常规的Python包。如果你对这些东西还不熟悉,我建议你先阅读一些Python入门课程。你可以尝试 http://diveintopython.org/toc/index.html 或者 http://docs.python.org/tutorial/index.html 作为一个很好的起点。 - abbot

4

很抱歉,但是这个模块虽然能正常工作,但几乎没有文档,而且它的布局对于那些喜欢在安装后使用“>> help(模块名称)”的人来说有点令人困惑。我将使用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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接