Flask-Ask:如何修复'flask.debughelpers.FormDataRoutingRedirect'错误?

时间:2019-05-15 11:22:04

标签: python python-3.x flask flask-ask

我正在用Flask-Ask开发Al​​exa Skill。 当我测试自己的技能时,会引发错误。

我将Nginx代理用于Flask-Ask。

(以前,它在另一台服务器上可以正常工作。)现在,另一台服务器也有相同的问题。 因此,我认为还有另一个问题,而不是代码。 我还看到了Flask Message Flashing error - flask.debughelpers.FormDataRoutingRedirect,但没有解决。

代码:

from flask import Flask
from flask_ask import Ask, statement

app = Flask(__name__)
ask = Ask(app, '/')

@ask.launch
def launch():
    speech_text = "Hello!"
    return statement(speech_text).simple_card('Hello', speech_text)

if __name__ == '__main__':
    app.run(debug=True)

控制台输出:

127.0.0.1 - - [15/May/2019 10:41:03] "POST  HTTP/1.0" 500 -
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1590, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1576, in raise_routing_exception
    raise FormDataRoutingRedirect(request)
flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/".  Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.  Note: this exception is only raised in debug mode'
127.0.0.1 - - [15/May/2019 10:41:05] "POST  HTTP/1.0" 500 -
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1590, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/user/.local/lib/python3.7/site-packages/flask/app.py", line 1576, in raise_routing_exception
    raise FormDataRoutingRedirect(request)
flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/".  Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.  Note: this exception is only raised in debug mode'

nginx配置:

server {
    listen       80;
    server_name  mydomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen       443 ssl;
    server_name  mydomain.com;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;

    location / {
        deny  all;
    }

    location /alexa {
        proxy_pass http://127.0.0.1:5000/;
    }
}

我的环境:

  • Ubuntu 19.04
  • Python 3.7.3
  • nginx / 1.15.12

1 个答案:

答案 0 :(得分:1)

要解决此问题...

1。编辑Nginx配置

/删除结尾的proxy_pass

之前
proxy_pass http://127.0.0.1:5000/;
proxy_pass http://127.0.0.1:5000;

2。编辑python代码

它需要与nginx config location /alexa {匹配。

之前
ask = Ask(app, '/')
ask = Ask(app, '/alexa/')

3。 (选项?)重新安装pyOpenSSL

如果您有这样的错误...

AttributeError: module 'lib' has no attribute 'X509V3_EXT_get'

重新安装pyOpenSSL。 我安装了pyOpenSSL 19.0.0。

终奌站
pip3 uninstall pyOpenSSL
pip3 install pyOpenSSL