【问题标题】:I use cx_freeze with my flask app, but always get ERR_EMPTY_RESPONSE, why?我在我的烧瓶应用程序中使用 cx_freeze,但总是得到 ERR_EMPTY_RESPONSE,为什么?
【发布时间】:2016-02-08 16:30:50
【问题描述】:

我使用 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'])

starter.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: 哦,我怕我弄错了。原因 500 错误处理程序也是由模板编写的。在我修复它之后,真正的错误是“找不到模板:index.html”

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

【问题讨论】:

    标签: python flask cx-freeze


    【解决方案1】:

    终于找到解决办法了。

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

      import os
      from IndividualWebsite import app
      
      abs_path = os.path.abspath('.')
      app.template_folder = abs_path + '/templates'
      
      app.run()
      
    • 我也使用烧瓶引导程序。所以我必须包括烧瓶引导的模板和宏。我所做的是将它们全部复制到我的模板文件夹中,并使我自己的模板依赖于副本。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-05
      • 2020-09-04
      • 2019-04-08
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2017-09-05
      • 2020-09-06
      相关资源
      最近更新 更多