gpt4 book ai didi

python - 带有 django-recaptcha 的新 Google ReCAPTCHA

转载 作者:太空宇宙 更新时间:2023-11-04 01:09:19 25 4
gpt4 key购买 nike

是否有一种简单的方法来调整 django-recaptcha 插件以使用新的 google re-captcha API?我已经更改了模板,验证码以写入方式显示,但验证无效。有谁知道我可以如何以简单的方式解决这个问题?

最佳答案

或者自己制作:这是我使用其他人作为模板/起点制作的。

import json
import urllib2, urllib

API_SERVER = "https://www.google.com/recaptcha/api/siteverify"


class RecaptchaResponse(object):
def __init__(self, is_valid, error_code=None):
self.is_valid = is_valid
self.error_code = error_code

def __unicode__(self):
print "%s, %s" % (self.is_valid, self.error_code)

def __str__(self):
return self.__unicode__()

def get_error(self):
if self.error_code and len(self.error_code):
return self.error_code[0]


def submit(recaptcha_secret, recaptcha_response, remoteip=''):

if not (recaptcha_secret and recaptcha_response and len(recaptcha_secret) and len(recaptcha_response) ):
return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')

def encode_if_necessary(s):
if isinstance(s, unicode):
return s.encode('utf-8')
return s

params = urllib.urlencode({
'secret': encode_if_necessary(recaptcha_secret),
'response': encode_if_necessary(recaptcha_response),
'remoteip': encode_if_necessary(remoteip)
})

request = urllib2.Request(
url=API_SERVER,
data=params,
headers={
"Content-type": "application/x-www-form-urlencoded",
"User-agent": "reCAPTCHA Python"
}
)

httpresp = urllib2.urlopen(request)
return_values = json.loads(httpresp.read())
print return_values

if return_values.get('success', False):
return RecaptchaResponse(is_valid=True)
else:
return RecaptchaResponse(is_valid=False, error_code=return_values.get('error-codes', ''))

关于python - 带有 django-recaptcha 的新 Google ReCAPTCHA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28657935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com