首页 > 解决方案 > 烧瓶输出损坏且不完整

问题描述

当我在我的应用程序中打开某个页面时,输出会返回完整的结果,但对于其他一些页面(从格式角度来看是完全相同的),输出结果会被截断(即,它没有向我显示完整的页面. 另外,在某些情况下,它甚至不显示输出结果。

----- .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 %}

标签: pythonflask

解决方案


您应该阅读以下规格send_file()

https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_file

  1. 文件路径应该相对于您的应用程序的根目录,而不是相对于您计算机的登录用户。
  2. attachment_filename应该被忽略,否则它应该有.txt扩展名,所以 mimetype 是正确的
  3. 由于您的应用程序不断地覆盖同一个文件,您可能应该将 设置cache_timeout为一个较小的值。否则,一些重复用户可能会看到旧版本。
  4. 在占位符字符串周围加上引号。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()

推荐阅读