gpt4 book ai didi

python - WSGI在应用程序的同一目录中找不到文件

转载 作者:行者123 更新时间:2023-12-04 18:45:32 26 4
gpt4 key购买 nike

我正在尝试使用 apache2 和 wsgi 设置 Ubuntu 服务器,但出现 500 内部错误。 WSGI 找不到文件“client_secrets.json”。

应用结构

/var/www/FlaskApp/
flaskapp.wsgi
- FlaskApp
- static
// All js, css, imgs and fonts
- templates
// All templates in here
__init__.py
client_secrets.json
database_setup.py
starter_content.py

初始化 .py
app = Flask(__name__)

CLIENT_ID = json.loads(
open('client_secrets.json', 'r').read())['web']['client_id']
APPLICATION_NAME = "Catalog App"

if __name__ == '__main__':
app.secret_key = 'super_secret_key'
app.debug = True
app.run(host='0.0.0.0', port=5000)

flask 应用程序.wsgi:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'MY_SECRET_KEY'

/etc/apache2/sites-available/FlaskApp.conf :
<VirtualHost *:80>
ServerName SERVER_IP
ServerAdmin email@email.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

尾 -f/var/log/apache2/error.log:
error.png

最佳答案

您必须提供json的正确路径文件,你可以试试 open_resource :

with app.open_resource('client_secrets.json') as f:    
CLIENT_ID = json.load(f)['web']['client_id']

或直接使用路径:
import os
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
json_url = os.path.join(PROJECT_ROOT, 'client_secrets.json')
CLIENT_ID = json.load(open(json_url))['web']['client_id']

关于python - WSGI在应用程序的同一目录中找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44742566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com