我使用python 3.4和最新的flask 0.10.1、flaskbootstrap3.3.5.6、flaskwtf0.12、Jinja2.8来制作我的web应用程序。
现在我尝试使用cx_freeze将我的应用程序转换为.exe。
我的应用程序在python中运行良好。但冻结后,浏览器会得到ERR_EMPTY_响应。
我写了一个简单的测试用例,它也有同样的问题。
这个问题花了我好几个小时。可能是使用模板造成的吗?谁能帮我?
这是我的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”。
但是我把所有模板都包含在StupU.Py中,它们存在于“构建/XXX/模板”目录中。为什么?
最佳答案
最后我找到了解决办法。
我将flask app的模板文件夹重置为starter.py中的“./templates”:
import os
from IndividualWebsite import app
abs_path = os.path.abspath('.')
app.template_folder = abs_path + '/templates'
app.run()
我也用烧瓶靴带。所以我必须包含flask引导程序的模板和宏。我所做的是将它们全部复制到我的模板文件夹中,并使我自己的模板依赖于副本。
希望这些能对想发布烧瓶应用的人有所帮助。
关于python - 我在我的 flask 应用程序中使用了cx_freeze,但是总是得到ERR_EMPTY_RESPONSE,为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33585224/