言って申し訳ありませんが、このモジュール、それだけで正常に動作している間、ほぼ完全に文書化されていないで、それはレイアウトだが、それらのために混乱少しありますインストール後に「>> 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
がHi修道院長、 私は、Pythonに新しいです、あなたはどのように説明することができます魔法はありません
は、モジュールだけでドキュメントを次のダウンロードしたパッケージを詳細に使用しますか? –
通常のpythonパッケージとしてインストールする必要があります。 これらのことを初めて知りたければ、Pythonの入門コースを読むことをお勧めします。良い出発点としてhttp://diveintopython.org/toc/index.htmlまたはhttp://docs.python.org/tutorial/index.htmlを試すことができます。 – abbot