2017-06-16 94 views
0

我有一个瓶服务器,如:瓶404网址

from flask import Flask 
app = Flask(__name__, static_url_path='') 

@app.route('/') 
def server_index(): 
    return app.send_static_file('ngrok_run_a_public_server.html') 


if __name__ == '__main__': 
    app.run() 

我把它像一个文件夹中:

examples 
    -ngrok_run_a_public_server.html 
    -server.py 

它们都在同一个文件夹中,这是怎么了我看到其他人提供静态文件。我在本地和ngrok上获得404。我只想公开服务器文件。

它使用http://127.0.0.1:5000/http://127.0.0.1:5000都失败。

回答

1

只需在示例文件夹内创建一个名为static的文件夹,并将其移动到那里。它应该工作。

1

你可以在根目录下创建一个文件夹,任何你想要的名字 - 比方说'myDailyReports'。并且该文件夹中的文件被称为'Day1.xlsx'。

import os 
from flask import send_from_directory, current_app 

@app.route('/') 
def server_index():  

    path = os.path.join(current_app.root_path, 'myDailyReports') 
    filename = 'Day1.xlsx' 

    # returns the file to download as an attachment with as_attachment as True 
    return send_from_directory(directory=path, filename=filename, as_attachment=True) 

如果你不想下载,而只是希望在浏览器中显示文件内容,也许.txt.html文件 -

return send_from_directory(directory=path, filename=filename) 
+0

为什么不为之工作,如果我设置'static_url_path = '''? – codyc4321