2017-06-16 92 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