유효성 검사를 위해 Python Plugin Recaptcha 클라이언트를 사용하는 방법은 무엇입니까?
-
10-07-2019 - |
문제
보안물 검증을하고 싶습니다.
나는 키를 얻습니다 Recaptcha 웹 사이트 그리고 이미 도전 과제로 웹 페이지를로드하기 위해 공개 키를 넣는 데 성공했습니다.
<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
다른 팁
죄송합니다. 그러나이 모듈은 잘 작동하지만 거의 완전히 문서화되지 않으며 설치 후 ">> help (modulename)"를 선호하는 사람들에게 혼란스러운 레이아웃입니다. 나는 Cherrypy를 사용하여 예제를 제시하고 나중에 CGI 관련 댓글을 작성하겠습니다.
Captcha.py에는 두 가지 기능과 클래스가 포함되어 있습니다.
display_html : 친숙한 "recaptcha box"를 반환합니다.
제출 : 백그라운드에서 사용자가 입력 한 값을 제출하는 것은
RecapcharPonse : 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의 요청을 사용한 Forevddr Environment 변수를 사용하고 필드 스토리지를 사용하여 수표를 수행하는 경우 거의 동일합니다.
마법은 없으며 모듈은 문서를 따릅니다.https://developers.google.com/recaptcha/docs/display
처리해야 할 유효성 검사 오류 :https://developers.google.com/recaptcha/docs/verify