首页 > 解决方案 > 烧瓶 send_static_file - 未找到错误

问题描述

试图让它与在我以前的问题上标记的重复问题一起工作,提供静态文件

但我只得到这个:

Not Found
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.

这是我的代码:

app = Flask(__name__, static_url_path='')

@app.route('/WilmaGetLogs/<name>"')
def WilmaGetLogs(name):
    path="cat /var/log/messages"
    tempStr=''
    HOST = name
    user = "root"
    password = "asdf"
    print('Test')
    tn = telnetlib.Telnet(HOST, timeout=5)
    tn.read_until(b"login: ")
    time.sleep(1)
    tn.write(user.encode('ascii') + b"\n")
    tn.read_until(b'Password: ')
    time.sleep(1)
    tn.write(password.encode('ascii') + b"\n")
    time.sleep(2)
    tn.write(path.encode('ascii') + b"\n")
    tn.write(b"exit\n")
    tempStr=tn.read_all().decode('ascii')
    text_file=open("output.txt","w")
    text_file.write(tempStr)
    text_file.close()
    return app.send_static_file("output.txt")

文件 output.txt 在 .py 文件正在运行的目录中创建,并且我的静态路径设置为 '' 所以它应该使用正在运行的程序的根目录 - 或者不?

我想要做的是:在计算机上下载文件,我按下按钮执行 WilmaGetLogs - 而不是在服务器上

标签: pythonflask

解决方案


我现在设法发送文件return send_from_directory("./", "output.txt", as_attachment=True)- 仍然不知道为什么 send_static_file 不起作用,但使用 send_from_directory 我能够将它发送到单击网页上的按钮并获得自动文件下载输出的客户端作为响应。文本


推荐阅读