RECAPTCHA_PUBLIC_KEY配置未使用Flask-WTForms设置

时间:2015-06-27 22:02:11

标签: python flask recaptcha wtforms flask-wtforms

我已经反复阅读文档,并且当联系页面尝试使用带有wtforms的Recaptcha加载Flask时,仍然会收到'RuntimeError:RECAPTCHA_PUBLIC_KEY config not set'。我愿意接受任何建议。我使用Google的ReCaptcha 2,FWIW在Ubuntu 14.04上运行Flask。

编辑:此问题已通过dirn的建议解决,但我现在收到KeyError消息,尽管正确输入了公钥和私钥。

这是我的堆栈跟踪。

{% extends "layout.html" %}

{% block body %}
<h2>Contact</h2>

{% if success %}
  <p>Thank you for your message. We'll get back to you shortly.</p>
  <p>Return to <a href="{{ url_for('home') }}">Home Page</a>.</p>

{% else %}

  {% for message in form.name.errors %}
    <div class="flash">{{ message }}</div>
  {% endfor %}

  {% for message in form.email.errors %}
    <div class="flash">{{ message }}</div>
  {% endfor %}

  {% for message in form.subject.errors %}
    <div class="flash">{{ message }}</div>
  {% endfor %}

  {% for message in form.message.errors %}
    <div class="flash">{{ message }}</div>
  {% endfor %}

  <form action="{{ url_for('contact') }}" method=post>
    {{ form.hidden_tag() }}

    {{ form.name.label }}
    {{ form.name }}

    {{ form.email.label }}
    {{ form.email }}

    {{ form.subject.label }}
    {{ form.subject }}

    {{ form.message.label }}
    {{ form.message }}

    {{ form.recaptcha }}

    {{ form.submit }}
  </form>

{% endif %}
{% endblock %}

forms.py

public class Server {
//Atributes....................................
private ServerSocket serverSocket = null;   
private Socket socket = null;

public boolean conected = false;

 private ObjectInputStream Mensajeinput = null;
 private ObjectOutputStream Mensajeoutput = null;

 private DataInputStream BomberInput = null;
 private DataOutputStream BomberOutput = null;
//.............................................

 int X, Y;
 BufferedImage CurrentSprite;

public static void main(String[] args) throws Exception {
   Server Servidor = new Server();

   Servidor.run();
}

public void run() throws Exception{

        serverSocket = new ServerSocket(208);     

        socket = serverSocket.accept();  


        conected=true;                                       
        Mensaje Actualization= null;                 

        while ( conected ){

            //=====initialize all the input and output Streams================================

              BomberInput = new DataInputStream(socket.getInputStream());
              BomberOutput = new DataOutputStream(socket.getOutputStream());

              Mensajeinput = new ObjectInputStream(socket.getInputStream());
              Mensajeoutput = new ObjectOutputStream(socket.getOutputStream());

              //=====receive and send the ints ============

              X = (int) BomberInput.readInt();
              BomberOutput.writeInt(X);
              BomberOutput.flush();

              Y = (int) BomberInput.readInt();
              BomberOutput.writeInt(Y);
              BomberOutput.flush();


            //=========receieve and send the image===================== 
              CurrentSprite = (BufferedImage) ImageIO.read(BomberInput);
              ImageIO.write(CurrentSprite, "GIF", BomberOutput);

            //==========receive the object=================

              Actualization= (Mensaje) Mensajeinput.readObject();        

        }
        //====Close socket===============================
        socket.close();

  }
}

run.py

  M = new Mensaje(0 , 0 , 0, 0);

                if (b1.isAlive()) controlPj.MoverPj(b1, M);


 //*****************************INTERACTION WITH THE SERVER*****************************                    
               //=====Initialize inputs and outputs=======

                 BomberOutput = new DataOutputStream(Sock.getOutputStream());
                 BomberInput = new DataInputStream(Sock.getInputStream());

                 MensajeOutput = new ObjectOutputStream(Sock.getOutputStream());
                 MensajeInput = new ObjectInputStream(Sock.getInputStream());

                //=====Sends and receive the ints =======  
                 BomberOutput.writeInt(b1.x);
                 X = BomberInput.readInt();
                 BomberOutput.flush();


                 BomberOutput.writeInt(b1.y);
                 Y = BomberInput.readInt();
                 BomberOutput.flush();

                 //======Sends and receive the image=======

                     ImageIO.write(b1.Current, "GIF",  BomberOutput );
                     EnemmySprite = (BufferedImage) ImageIO.read(BomberInput);

                 //======================Sends the object==================

                   MensajeOutput.writeObject(M);
                   MensajeOutput.flush();

                  System.out.println("Matriz Actualizada Recibida");
               //==========================================================================

//***********************************************************************************************************************          

contact.html

  Exception in thread "main" java.io.StreamCorruptedException: invalid type code: 3B
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1379)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at bomberman2.Server.run(Server.java:90)
    at bomberman2.Server.main(Server.java:42)

1 个答案:

答案 0 :(得分:6)

您没有将配置设置添加到应用程序。您正在定义模块级变量

RECAPTCHA_USE_SSL = False
RECAPTCHA_PUBLIC_KEY = 'public'
RECAPTCHA_PRIVATE_KEY = 'private'
RECAPTCHA_OPTIONS = {'theme': 'white'}

您需要使用名称作为配置字典的键。

app.config['RECAPTCHA_USE_SSL'] = False
app.config['RECAPTCHA_PUBLIC_KEY'] = 'public'
app.config['RECAPTCHA_PRIVATE_KEY'] = 'private'
app.config['RECAPTCHA_OPTIONS'] = {'theme': 'white'}