Dominik Lemberger
Dominik Lemberger

Reputation: 2426

Flask send_static_file - Not found error

Tried to get it work with the duplicated Questions that got marked on my previous questions serve static files

But I only get this:

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

Here is my code:

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")

The File output.txt gets created in the directory the .py file is running and my static path is set to '' so it should use the root directory of the program running - or not?

What I want to do is: get the file on the computer as download, where I press the Button to execute WilmaGetLogs - not on the server

Upvotes: 1

Views: 956

Answers (1)

Dominik Lemberger
Dominik Lemberger

Reputation: 2426

I managed sending the file now with return send_from_directory("./", "output.txt", as_attachment=True) - still do not know why the send_static_file does not work, but with the send_from_directory I was able to send it to the client that clicked the button on the webpage and got as response an automatic file download of output.txt

Upvotes: 1

Related Questions