python - 我在我的 flask 应用程序中使用 cx_freeze,但总是得到 ERR_EMPTY_RESPONSE,为什么?

标签 python flask cx-freeze

我使用 python 3.4 和最新的 flask 0.10.1、flask-bootstrap 3.3.5.6、flask-wtf 0.12、Jinja2 2.8 来制作我的网络应用程序。

现在我尝试使用 cx_freeze 将我的应用程序转换为 .exe。

我的应用程序在 python 中运行良好。但是在卡住之后,浏览器得到 ERR_EMPTY_RESPONSE。

我写了一个简单的测试用例,它也有同样的问题。

这个问题花了我几个小时。可能是使用模板引起的?谁能帮帮我?

这是我的setup.py

from cx_Freeze import setup, Executable

includefiles = ['templates/', 'static/']

base = None

main_executable = Executable("starter.py", base=base, copyDependentFiles=True)

setup(name="Example",
      version="0.1",
      description="Example Web Server",
      options={
      'build_exe': {
          'packages': ['jinja2',
                       'jinja2.ext',
                       'flask_wtf',
                       'flask_bootstrap',
                       'os'],
          'include_files': includefiles,
          'include_msvcr': True}},
      executables=[main_executable], requires=['flask', 'wtforms'])

启动器.py:

from IndividualWebsite import app

app.run()

个人网站.py:

from flask import Flask, render_template, request, session, redirect, url_for, flash

from flask_bootstrap import Bootstrap
import jinja2.ext

from flask_wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
bootstrap = Bootstrap(app)


class NameForm(Form):
    name = StringField('What is your name?', validators=[Required()])
    submit = SubmitField('Submit')


@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('Looks like you have changed your name!')
        session['name'] = form.name.data
        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'))

我的 index.html 是 jinja2 模板:

{% extends 'base.html' %}
{% import "bootstrap/wtf.html" as wtf %}

{% block page_content %}
<div class="page-header">
    <h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}

基本模板正在使用 boostrap 模板。

更新 1: 卡住的服务器只显示

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

没有别的

更新 2: 如果我只返回一个字符串:

return '<h1>Test</h1>'

服务器可以响应正确的内容。可能是模板引起的。

更新 3: 哦,恐怕我弄错了。 Cause 500 error hanlder也是模板写的。修复后,真正的错误是“找不到模板:index.html”

但我在 setup.py 中包含了所有模板,它们存在于“build/xxx/templates”目录中。为什么?

最佳答案

终于找到解决办法了。

  • 我在 starter.py 中将 flask 应用程序的模板文件夹重置为“./templates”:

    import os
    from IndividualWebsite import app
    
    abs_path = os.path.abspath('.')
    app.template_folder = abs_path + '/templates'
    
    app.run()
    
  • 我也使用 flask-bootstrap。所以我必须包括 flask-bootstrap 的模板和 macroses。我所做的是将它们全部复制到我的模板文件夹中,并根据副本制作我自己的模板。

希望这些对想要分发 Flask 应用程序的人有用。

关于python - 我在我的 flask 应用程序中使用 cx_freeze,但总是得到 ERR_EMPTY_RESPONSE,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585224/

相关文章:

python - 想要从 pandas/python 中的数据帧创建像数据帧一样的稀疏矩阵

python - 全局变量和 python flask

python - Flask-SQLAlchemy:没有外键链接这些表

python - cx_Freeze 未检测到 cv2

Python:构建后出现 CX_Freeze 问题

Python 3.3.4 Cx_Freeze 导入错误 : DDL load failed: The specified module could not be found

python - 添加具有不同维数的数组

python - 在 python3 中使用 json.dump 无效的 Json

python - 如何在tomcat服务器上运行python文件?

python - 如何使用 Flask Request 将 GET 参数传递给 url