Skip to main content
 首页 » 编程设计

python之如何在没有 Flask-WTF 的情况下使用 recaptcha

2025年02月15日8mate10pro

我制作一个文件共享网站已经有一段时间了,我想在人们注册到该网站时实现 recaptcha。问题是我不能使用 Flask-WTF,因为我将不得不更改很多代码(我一直在没有它的情况下编程)。

我发现这个 Flask recaptcha 不包括 Flask-WTF 的使用,但我似乎无法让它工作(它不显示 recaptcha 本身):

https://github.com/mardix/flask-recaptcha

我已经一步步跟着做了,还是不行。我唯一没有做的是配置。

编辑: 验证码不工作。每次我输入正确的注册信息并标记验证码时,它都会说用户名/密码不正确。如果我不标记它,它也会做同样的事情。

这是验证码(其他人之前工作过):

recaptcha = ReCaptcha(app=app) 
 
if recaptcha.verify() is False: 
            flash('Captcha is incorrect') 
            return redirect(url_for('register')) 
 
    <div id="captcha""> 
        {{ recaptcha }} - HTML PART 
    </div> 

编辑:在得到 Nurzhan 的帮助后,我更改了代码,无论如何验证码总是返回 false。

请您参考如下方法:

您没有尝试配置,但您需要指明 key 才能使您的 recaptcha 正常工作。这两个选项在配置中不是可选的:

RECAPTCHA_SITE_KEY : Public key 
 
RECAPTCHA_SECRET_KEY: Private key 

为它们设置适当的值,然后看看它是否有效。

编辑:

它现在正在工作。这是 app.py:

import requests 
import json 
from flask import Flask, render_template, request 
from flask_recaptcha import ReCaptcha 
 
app = Flask(__name__) 
 
app.config.update({'RECAPTCHA_ENABLED': True, 
                   'RECAPTCHA_SITE_KEY': 
                       'site_key', 
                   'RECAPTCHA_SECRET_KEY': 
                       'secret_key'}) 
 
 
recaptcha = ReCaptcha(app=app) 
 
 
@app.route('/') 
def index(): 
    return render_template('index.html') 
 
 
@app.route('/submit', methods=['GET', 'POST']) 
def submit(): 
    print('SUBMIT CALLED') 
    username = '' 
    password = '' 
 
    if request.method == 'POST': 
        username = request.form['username'] 
        password = request.form['password'] 
 
    print(request.form) 
 
    if username == 'username' and password == 'password': 
        print('CREDENTIALS ARE OK') 
 
        r = requests.post('https://www.google.com/recaptcha/api/siteverify', 
                          data = {'secret' : 
                                  'secret_key', 
                                  'response' : 
                                  request.form['g-recaptcha-response']}) 
 
        google_response = json.loads(r.text) 
        print('JSON: ', google_response) 
 
        if google_response['success']: 
            print('SUCCESS') 
            return render_template('profile.html') 
        else: 
            # FAILED 
            print('FAILED') 
            return render_template('index.html') 
 
 
#        if recaptcha.verify(): 
#            # SUCCESS 
 
app.run(debug=True) 

这是 index.html 页面:

<!DOCTYPE html> 
<html> 
  <head> 
    <script src='https://www.google.com/recaptcha/api.js'></script> 
  </head> 
<body> 
 
<h1>Flask Recaptcha</h1> 
 
<p>Flask Recaptcha Test</p> 
 
<form method="post" action="/submit"> 
  Username:<br> 
  <input type="text" name="username"><br> 
  Password:<br> 
  <input type="password" name="password"> 
 
  {{ recaptcha }} 
 
  <input type="submit" value="Submit"> 
  <div class="g-recaptcha" data-sitekey="site_key"></div> 
</form> 
 
</body> 
</html> 

如果通过验证,这是profile.html页面:

<!DOCTYPE html> 
<html> 
  <head> 
  </head> 
<body> 
 
<h1>Profile page</h1> 
 
<p>Registration is ok</p> 
 
</body> 
</html> 

我做不到recaptcha.verify()工作。在 Google Recaptcha 的官方文档中指出,在客户提交带有 secret_key 的表单后,您需要单独向 google recaptcha api 发送一个 post 请求。和 g-recaptcha-response当用户在 recaptcha 中打勾时,您会收到。

请注意,这只是示例代码。您需要将自己的 site_key 和 secret_key 添加到 app.py和 index.html并且还添加了对用户凭据的正确检查以进行注册,例如两次输入密码等。