def _validate_recaptcha(self, challenge, response, remote_addr):
"""Performs the actual validation."""
try:
private_key = current_app.config['RECAPTCHA_PRIVATE_KEY']
except KeyError:
raise RuntimeError("No RECAPTCHA_PRIVATE_KEY config set")
data = url_encode({
'privatekey': private_key,
'remoteip': remote_addr,
'challenge': challenge,
'response': response
})
response = http.urlopen(RECAPTCHA_VERIFY_SERVER, to_bytes(data))
if response.code != 200:
return False
rv = [l.strip() for l in response.readlines()]
if rv and rv[0] == to_bytes('true'):
return True
if len(rv) > 1:
error = rv[1]
if error in self._error_codes:
raise RuntimeError(self._error_codes[error])
return False
评论列表
文章目录