How to use Python plugin reCaptcha client for validation?

Hoang Pham picture Hoang Pham · Sep 17, 2009 · Viewed 12k times · Source

I want to make a captcha validation.

I get the key from the recaptcha website and already succeed to put the public key to load the webpage with the challenge.

<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>

I download the reCaptcha Python plugin but I can not find any documentation on how to use it.

Does anyone have any idea on how to use this Python plugin? recaptcha-client-1.0.4.tar.gz (md5)

Answer

abbot picture abbot · Sep 17, 2009

It is quite straightforward. This is an example from a trivial trac plugin I'm using:

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