python - 烧瓶输出损坏且不完整
问题描述
当我在我的应用程序中打开某个页面时,输出会返回完整的结果,但对于其他一些页面(从格式角度来看是完全相同的),输出结果会被截断(即,它没有向我显示完整的页面. 另外,在某些情况下,它甚至不显示输出结果。
----- .py 代码 ------
class SHCDCConfig:
def __init__(self, vlanid, description, vrf, ip, mask):
self.vlanid = vlanid
self.description = description
self.vrf = vrf
self.ip = ip
self.mask = mask
def vxlan_config(self):
return f"""
vlan {self.vlanid}
name {self.description}
vn-segment 12{self.vlanid}
exit\n
interface vlan {self.vlanid}
vrf member {self.vrf}
description {self.description}
ip address {self.ip} {self.mask}
fabric forwarding mode anycast-gateway
no ip redirects
no shut
exit\n
interface nve1
member vni 12{self.vlanid}
multisite ingress-replication
suppress-arp
ingress-replication protocol bgp
exit\n
exit\n
evpn
vni 12{self.vlanid} l2
rd auto
route-target import auto
route-target export auto
exit\n
"""
from flask import Flask, request, render_template
from SHCConfigFile import DistroConfig, SHCDCConfig, AccessSwitch
from flask import send_file
app = Flask(__name__)
@app.route('/vxlan_config', methods=["GET", "POST"])
def vxlan_config():
import os
vxlan_config_file = open("/Users/ahmad/ShcConfigs/vxlan_config.txt", "w")
if request.method == "POST":
vlanid = request.form["vlanid"]
description = request.form["description"]
vrf = request.form["vrf"]
ip = request.form["ip"]
mask = request.form["mask"]
vxlan_conf = SHCDCConfig(vlanid, description, vrf, ip, mask)
vxlan_config_file.write(vxlan_conf.vxlan_config())
return send_file("/Users/ahmad/ShcConfigs/vxlan_config.txt", attachment_filename='vxlan_config')
return render_template('vxlan_config.html')
-------- html 模板 ----------
{% extends 'base.html' %}
{% block content %}
<html>
<body>
<p><h3>Enter the Values Below:</h3></p>
<form method="post" action="/vxlan_config">
<p>VLAN ID: <input name="vlanid" placeholder=3400></p>
<p>VLAN Description: <input name="description" placeholder=CitrixVLAN></p>
<p>VRF Name: <input name="vrf" placeholder=GRN200></p>
<p>SVI IP Address: <input name="ip" placeholder=10.248.10.1></p>
<p>VLAN Subnet Mask: <input name="mask" placeholder=255.255.255.0></p>
<p><input type="submit" value="Generate Config" /></p>
</form>
</body>
</html>
{% endblock %}
解决方案
您应该阅读以下规格send_file()
:
https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_file
- 文件路径应该相对于您的应用程序的根目录,而不是相对于您计算机的登录用户。
attachment_filename
应该被忽略,否则它应该有.txt
扩展名,所以 mimetype 是正确的- 由于您的应用程序不断地覆盖同一个文件,您可能应该将 设置
cache_timeout
为一个较小的值。否则,一些重复用户可能会看到旧版本。 - 在占位符字符串周围加上引号。
placeholder=Citrix VLAN
是无效的
更新:
您可能遇到缓冲区问题。尝试:
import os
vxlan_config_file.write(vxlan_conf.vxlan_config())
vxlan_config_file.flush()
os.fsync(vxlan_config_file.fileno())
vxlan_config_file.close()
推荐阅读
- javascript - 如何从
- php - 将sql查询存储在列表中时PHP内存耗尽
- flutter - 参数类型'列表
?不能分配给参数类型“列表” ' - reactjs - AWS Cognito - 输入属性包括客户端的不可写属性 [completeNewPasswordChallenge]
- python - 我无法重新定位或反转字符串 python
- reactjs - 错误:样式道具需要从样式属性到值的映射,而不是 onUploadProgress axios React 时的字符串
- c# - GameObject 在加载时无响应
- django - 如何获得一个空白数量字段以更新为 0 并阻止我的条带付款崩溃?
- azure-api-management - Azure API 管理的可行开发模式是什么?
- themes - 如何从 Hugo 主题的投资组合小部件中删除项目?