2009-09-17 15 views
15

Bir captcha doğrulaması yapmak istiyorum.Python eklentisi reCaptcha istemci doğrulama için nasıl kullanılır?

Anahtarı, recaptcha website numaralı anahtardan alıyorum ve zaten web sayfasının yüklenmeyle yüklenmesi için ortak anahtarı koymayı başardım.

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

Ben the reCaptcha Python plugin indirmek ama bunu nasıl kullanılacağı hakkında herhangi bir belge olamaz.

Bu Python eklentisinin nasıl kullanılacağı hakkında bir fikri olan var mı? recaptcha-client-1.0.4.tar.gz (md5)

cevap

25

Oldukça basittir. gayet güzel çalışıyor iken neredeyse tamamen belgesiz olduğunu,

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

Merhaba başrahip, Python için yeni, nasıl açıklarsınız sihirli yoktur

, modül sadece belgeler aşağıda İndirilen paketi daha ayrıntılı olarak kullanın? –

+2

Normal bir python paketi olarak yüklemelisiniz. Tüm bunlara yeniyseniz, python ile ilgili bir giriş dersi okumanızı tavsiye ederim. Http://diveintopython.org/toc/index.html veya http://docs.python.org/tutorial/index.html yi iyi bir başlangıç ​​noktası olarak deneyebilirsiniz. – abbot

4

Maalesef söylemek, ancak bu modül ve düzeni biraz olanlar için kafa karıştırıcı açıklanmıştır: Bu benim kullandığım bir önemsiz trac eklentisinden bir örnektir kurulumdan sonra ">> yardım (modulename)" kullanmayı tercih eden Cherrypy kullanarak bir örnek vereceğim ve daha sonra bazı cgi ile ilgili yorumlar yapacağım.

  • display_html:

    captcha.py iki işlev ve bir sınıf içeren tanıdık "ReCaptcha kutu"

  • döndürür gönderin: Arka planda

    kullanıcı tarafından girilen değerleri eğen
  • RecapchaResponse:

012 reCAPTCHA'yı

yanıtı içeren bir kap sınıf

İlk olarak, tüm yolu capcha.py'ye aktarmanız, ardından da yanıtı görüntüleyen ve ele alan bir dizi işlev oluşturmanız gerekir.

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) 

Sadece ben Çeklerinizin yapmak cherrypy en request.headers ve kullanım alanı depolama kullanılan REMOTE_ADDR ortam değişkenini kullanmak, hemen hemen cgi kullanılıyorsa aynı olacak. https://developers.google.com/recaptcha/docs/display

Doğrulama hataları işlemek gerekebilir: https://developers.google.com/recaptcha/docs/verify

İlgili konular