2009-09-17 8 views
15

私はcaptcha検証をしたいと思います。検証のためにPythonプラグインreCaptchaクライアントを使用するには?

私はrecaptcha websiteから鍵を取得しています。公開鍵を公開してチャレンジでWebページを読み込むことに成功しました。

<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

がHi修道院長、 私は、Pythonに新しいです、あなたはどのように説明することができます魔法はありません

は、モジュールだけでドキュメントを次のダウンロードしたパッケージを詳細に使用しますか? –

+2

通常のpythonパッケージとしてインストールする必要があります。 これらのことを初めて知りたければ、Pythonの入門コースを読むことをお勧めします。良い出発点としてhttp://diveintopython.org/toc/index.htmlまたはhttp://docs.python.org/tutorial/index.htmlを試すことができます。 – abbot

4

言って申し訳ありませんが、このモジュール、それだけで正常に動作している間、ほぼ完全に文書化されていないで、それはレイアウトだが、それらのために混乱少しありますインストール後に「>> help(modulename)」を使うことを好む私たちのことです。私はcherrypyを使って例を挙げ、後でいくつかのCGI関連のコメントを作成します。

captcha.pyは2つの機能とクラスが含まれています

  • display_html:おなじみの「reCAPTCHAの箱」

    を返し
  • は提出:バックグラウンドでユーザーが入力した値を提出した

  • RecapchaResponse:reCaptchaからの応答を含むコンテナクラス

まず、完全なパスをcapcha.pyにインポートして、レスポンスの表示と処理を処理する2つの関数を作成する必要があります。

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

関連する問題