2009-09-17 5 views
15

Voglio fare una convalida captcha.Come utilizzare il client reCaptcha del plugin Python per la convalida?

Ottengo la chiave dal recaptcha website e già riesco a mettere la chiave pubblica per caricare la pagina web con la sfida.

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

mi scaricare the reCaptcha Python plugin ma non riesco a trovare alcuna documentazione su come usarlo.

Qualcuno ha qualche idea su come usare questo plugin Python? recaptcha-client-1.0.4.tar.gz (md5)

risposta

25

È abbastanza semplice. Questo è un esempio da un plugin banale trac sto usando:

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

Hi Abate, Sono nuovo di Python, si può spiegare come utilizzare il pacchetto scaricato in maggiori dettagli? –

+2

Dovresti installarlo come un normale pacchetto python. Ti consiglio di leggere un corso introduttivo su Python se sei nuovo a tutte queste cose. Puoi provare http://diveintopython.org/toc/index.html o http://docs.python.org/tutorial/index.html come un buon punto di partenza. – abbot

4

Mi spiace dirlo, ma questo modulo, mentre funziona bene, è quasi del tutto privi di documenti, ed è il layout è un po 'di confusione per coloro di noi che preferiscono usare ">> help (modulename)" dopo l'installazione. Darò un esempio usando cherrypy e poi fare alcuni commenti relativi a cgi.

captcha.py contiene due funzioni e una classe:

  • display_html: che restituisce il familiare "scatola reCaptcha"

  • presentare: che presenta i valori inseriti dall'utente in background

  • RecapchaResponse: che è una classe contenitore che contiene la risposta dal reCaptcha

Per prima cosa è necessario importare il percorso completo in capcha.py, quindi creare un paio di funzioni che gestiscono la visualizzazione e la gestione della risposta.

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) 

Sarà più o meno lo stesso se si utilizza CGI, basta usare la variabile d'ambiente REMOTE_ADDR dove ho usato request.headers e utilizzo campo di stoccaggio di cherrypy per fare i vostri controlli.

Non c'è magia, il modulo segue solo la documentazione: https://developers.google.com/recaptcha/docs/display

errori di convalida si potrebbe aver bisogno di gestire: https://developers.google.com/recaptcha/docs/verify

Problemi correlati