問題描述
在成功的部署Python flask應(yīng)用到App Service (Windows)后漆撞,如果需要把當(dāng)前項(xiàng)目(如:hiflask)作為一個(gè)子項(xiàng)目(子站點(diǎn)),把web.config文件從wwwroot中移動(dòng)到項(xiàng)目文件夾中挚歧。訪問時(shí)扛稽,確遇見了404 Not Found的錯(cuò)誤。
查看flask項(xiàng)目的啟動(dòng)日志滑负,可以看見項(xiàng)目啟動(dòng)已經(jīng)成功庇绽。但是為什么請(qǐng)求一直都是404的問題呢?
2021-09-10 05:29:58.224796: wfastcgi.py will restart when files in D:\home\site\wwwroot\hiflask\ are changed: .*((\.py)|(\.config))$
2021-09-10 05:29:58.240445: wfastcgi.py 3.0.0 initialized
問題解決
在搜索 flask return 404問題后橙困,發(fā)現(xiàn)是因?yàn)?URL前綴(prefixed )的問題。因?yàn)楫?dāng)前的flask應(yīng)用訪問路徑不再是根目錄(/)耕餐,而是(/hiflask)凡傅。 所以需要 flask項(xiàng)目中使用 中間件 來(lái)處理 url前綴的問題。
原文內(nèi)容為:https://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes/36033627#36033627
You have to do is to write a middleware to make the following changes:
- modify PATH_INFO to handle the prefixed url.
- modify SCRIPT_NAME to generate the prefixed url.
Like this:
class PrefixMiddleware(object):
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
if environ['PATH_INFO'].startswith(self.prefix):
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
environ['SCRIPT_NAME'] = self.prefix
return self.app(environ, start_response)
else:
start_response('404', [('Content-Type', 'text/plain')])
return ["This url does not belong to the app.".encode()]
Wrap your app with the middleware, like this:
from flask import Flask, url_for
app = Flask(__name__)
app.debug = True
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/foo')
@app.route('/bar')
def bar():
return "The URL for this page is {}".format(url_for('bar'))
if __name__ == '__main__':
app.run('0.0.0.0', 9010)
Visit http://localhost:9010/foo/bar,
You will get the right result: The URL for this page is /foo/bar
而在App Service中的解決方式就是添加了 PrefixMiddleware肠缔,并指定prefix為 /hiflask 夏跷。 附上完成的hiflask/app.py 代碼和web.config內(nèi)容。
hiflask/app.py
from flask import Flask
from datetime import datetime
from flask import render_template
import re
class PrefixMiddleware(object):
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
if environ['PATH_INFO'].startswith(self.prefix):
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
environ['SCRIPT_NAME'] = self.prefix
return self.app(environ, start_response)
else:
start_response('404', [('Content-Type', 'text/plain')])
return ["This url does not belong to the app.".encode()]
#if __name__ =="__name__"
app = Flask(__name__)
app.debug = True
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/hiflask')
# @app.route("/")
# def home():
# return "Hello, Flask!"
print("flask applicaiton started and running..." +datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
# Replace the existing home function with the one below
@app.route("/")
def home():
return render_template("home.html")
# New functions
@app.route("/about/")
def about():
return render_template("about.html")
@app.route("/contact/")
def contact():
return render_template("contact.html")
@app.route("/hello/")
@app.route("/hello/<name>")
def hello_there(name = None):
return render_template(
"hello_there.html",
name=name,
date=datetime.now()
)
@app.route("/api/data")
def get_data():
return app.send_static_file("data.json")
if __name__ == '__main__':
app.run(debug=True)
hiflask/web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="WSGI_HANDLER" value="app.app"/>
<add key="PYTHONPATH" value="D:\home\site\wwwroot\hiflask"/>
<add key="WSGI_LOG" value="D:\home\site\wwwroot\hiflask\hiflasklogs.log"/>
</appSettings>
<system.webServer>
<handlers>
<add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
</handlers>
</system.webServer>
</configuration>
最終效果(作為子項(xiàng)目部署完成成功明未,所以一個(gè)app service就可以部署多個(gè)站點(diǎn))
提醒一點(diǎn):App Service中如果要部署多站點(diǎn)槽华,需要在Configration 中配置 Virtual Applications。
參考資料:
https://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes/36033627#36033627
https://www.cnblogs.com/lulight/p/15220297.html
https://www.cnblogs.com/lulight/p/15227028.html
當(dāng)在復(fù)雜的環(huán)境中面臨問題趟妥,格物之道需:濁而靜之徐清猫态,安以動(dòng)之徐生。 云中披摄,恰是如此!
標(biāo)簽: flask項(xiàng)目作為子應(yīng)用的404問題, 使用Flask框架部署Python應(yīng)用, Python web.config, App Service, Python Flask 404 Not Found