简体   繁体   English

从Bottle读取uwsgi_param

[英]Read uwsgi_param from Bottle

I have a python3 application running under a nginx/uwsgi application. 我有一个在nginx / uwsgi应用程序下运行的python3应用程序。 It's a multi-environment app, so I decided to create diferent virtualhosts for each environment. 这是一个多环境的应用程序,因此我决定为每种环境创建不同的虚拟主机。 On each virtualhost I define the environment: 在每个虚拟主机上,我定义环境:

uwsgi_param APPLICATION_ENV dev;

But I cannot read from Bottle, I'm trying: 但是我无法从Bottle中读取内容,我正在尝试:

request.environ['APPLICATION_ENV']

or 要么

request.environ.get('APPLICATION_ENV')

I'm trying other solutions but none of them works, what am I doing wrong?? 我正在尝试其他解决方案,但都不起作用,我在做什么错?

I found a solution. 我找到了解决方案。

It is important to know that uwsgi_params will be loaded after a request is done, so this is why each time i execute request.environ I don't get the uwsgi value. 重要的是要知道uwsgi_params将在请求完成后加载,因此这就是为什么每次我执行request.environ时我都不会得到uwsgi值。 If I move this line to an @app.route, it works. 如果我将此行移动到@ app.route,它将起作用。

So, in order to fix this, I added a uswgi variable into my uwsgi_app.ini file: 因此,为了解决此问题,我在uwsgi_app.ini文件中添加了uswgi变量:

env = APPLICATION_ENV=dev

Then in your python code, before your app.run(), you can get the variable APPLICATION_ENV as follows: 然后,在python代码中,在app.run()之前,您可以按以下方式获取变量APPLICATION_ENV:

import os
application_env = os.getenv('APPLICATION_ENV', socket.gethostname())

Where socket.gethostname() is the default value if APPLICATION_ENV is not found. 如果未找到APPLICATION_ENV,则其中socket.gethostname()是默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM