検証のためにPythonプラグインreCaptchaクライアントを使用する方法は?
-
10-07-2019 - |
質問
キャプチャの検証を行いたい。
recaptcha Webサイトからキーを取得し、すでにチャレンジを含む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>
reCaptcha Pythonプラグインをダウンロードしましたが、その方法に関するドキュメントが見つかりませんこれを使って。
このPythonプラグインの使用方法について誰にもアイデアがありますか? recaptcha-client-1.0.4.tar.gz(md5)
解決
これは非常に簡単です。 これは私が使用している些細な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
他のヒント
申し訳ありませんが、このモジュールは正常に動作しますが、ほとんど文書化されていません。また、レイアウトは&quot;&gt;&gt;を使用することを好む私たちにとって少々混乱を招きます。 help(モジュール名)&quot;インストール後。 cherrypyを使用した例を挙げて、後でcgi関連のコメントを作成します。
captcha.pyには2つの関数と1つのクラスが含まれています:
-
display_html:おなじみの&quot; reCaptcha box&quot;を返します
-
submit:バックグラウンドでユーザーが入力した値を送信します
-
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を使用する場合もほぼ同じです。cherrypyのrequest.headersを使用したREMOTE_ADDR環境変数を使用し、フィールドストレージを使用してチェックを行います。
魔法はありません。モジュールはドキュメントに従います。 https://developers.google.com/recaptcha/docs/display
処理が必要な検証エラー: https://developers.google.com/recaptcha/docs/verify