2009-09-17 146 views
15

我想進行驗證碼驗證。如何使用Python插件reCaptcha客戶端進行驗證?

我從recaptcha website獲得密鑰,並已成功將公鑰加載到帶有挑戰的網頁。

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

我下載the reCaptcha Python plugin但我找不到任何有關如何使用它的文檔。

有沒有人有任何想法如何使用這個Python插件? recaptcha-client-1.0.4.tar.gz(md5)

回答

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

嗨方丈, 我是新來的Python,你能解釋一下如何更詳細地使用下載的軟件包? – 2009-09-17 23:06:21

+2

您應該將其安裝爲常規python包。 我建議你閱讀一些關於python的入門課程,如果你對所有這些東西都很陌生。你可以嘗試http://diveintopython.org/toc/index.html或http://docs.python.org/tutorial/index.html作爲一個很好的起點。 – abbot 2009-09-18 18:58:10

4

遺憾地說,但這個模塊,同時它工作得很好,幾乎完全是無證,它的佈局是一點點迷惑那些我們的人在安裝後更喜歡使用「>> help(modulename)」。我會舉一個使用cherrypy的例子,然後做一些與cgi相關的評論。

captcha.py包含兩個功能和一類:

  • display_html:返回熟悉的「驗證碼框」

  • 提交:它提交用戶在後臺

    輸入的值
  • 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相同,只是用REMOTE_ADDR環境變量,我使用CherryPy的request.headers和使用字段存儲做你檢查。

有沒有神奇的,模塊只是遵循文檔: https://developers.google.com/recaptcha/docs/display

驗證錯誤,你可能需要處理: https://developers.google.com/recaptcha/docs/verify

相關問題