如何使用Python插件reCaptcha客户端进行验证?
-
10-07-2019 - |
题
我想做一个验证码验证。
我从 验证码网站 并且已经成功地输入公钥来加载带有挑战的网页。
<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 包含两个函数和一个类:
显示_html:返回熟悉的“reCaptcha 框”
提交:它在后台提交用户输入的值
回顾回复:这是一个包含 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
不隶属于 StackOverflow