Where were you attempting to set the CE360AF_ENV environment variable? Apache
isn't going to read settings from your user account.
For the way you were trying to read it, you would have needed to have had
SetEnv directive for it in Apache configuration, but then what you were doing
of setting os.environ on each request is bad practice anyway. It also gets set
too late if trying to use it at global scope in module imports if the module
was imported before the first request.
What you should do instead is have a daemon mode process group and delegate the
WSGI application to it.
WSGIDaemonProcess dev
WSGIScriptAlias / /var/www/bbmetrics/bbmetrics.wsgi
process-group=dev application-group=${GLOBAL}
Then in your code use:
import mod_wsgi
deployEnv = mod_wsg.process_group == 'dev'
In other words, you key off the name of the process group the WSGI application
has been delegated to run in.
Graham
> On 15 Nov 2019, at 5:24 pm, Vaibhav Bajpai <[email protected]> wrote:
>
> Iam working on running a Python Flask app with Apache HTTP Server (v2.4)
> where Iam attempting to setup an environment variable to identify deploy
> environment type (Dev, Prod) and unable to extract the environment variable
> via "os.environ" in python code
>
> After following few posts, i made the following changes I made in key files
> of my code. Please help me with a solution. Any suggestions and workarounds
> would be appreciated
>
> bbmetrics.wsgi
>
> #!/usr/bin/python3
> import os
> import sys
> import logging
> logging.basicConfig(stream=sys.stderr)
> sys.path.insert(0, "/var/www/bbmetrics")
>
>
> def application(environ, start_response):
> os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
> from bbmetrics import app as _application
> application.secret_key = 'bbmetrics_secret'
> return _application(environ, start_response)
>
> I get the error as below when running the app Request URLs but the Python
> interpreter confirms that variable is correctly set
> mod_wsgi (pid=73322): Exception occurred processing WSGI script
> '/var/www/bbmetrics/bbmetrics.wsgi'.
> [Thu Nov 14 23:59:09.601165 2019] [wsgi:error] [pid 73322] [client
> 172.27.138.173:56868] Traceback (most recent call last):
> [Thu Nov 14 23:59:09.601199 2019] [wsgi:error] [pid 73322] [client
> 172.27.138.173:56868] File "/var/www/bbmetrics/bbmetrics.wsgi", line 10, in
> application
> [Thu Nov 14 23:59:09.601204 2019] [wsgi:error] [pid 73322] [client
> 172.27.138.173:56868] os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
> [Thu Nov 14 23:59:09.601223 2019] [wsgi:error] [pid 73322] [client
> 172.27.138.173:56868] KeyError: 'CE360AF_ENV'
>
> [dc-user@xxxxxx bbmetrics]$ python3
> Python 3.7.4 (default, Oct 25 2019, 03:53:56)
> [GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import os
> >>> print(os.environ['CE360AF_ENV'])
> dev
> >>>
>
> __init__.py
>
> #!/usr/bin/python3
> from flask import Flask
> app = Flask(__name__)
> from bbmetrics.bbmetrics import *
>
> Virtual Host Configiuration File
>
> <VirtualHost *:80>
> ServerName x.x.x.x
> ServerAlias xxxxx.com
> ServerAdmin [email protected]
> WSGIScriptAlias / /var/www/bbmetrics/bbmetrics.wsgi
> <Directory /var/www/bbmetrics/bbmetrics/>
> Order allow,deny
> Allow from all
> </Directory>
> Alias /static /var/www/bbmetrics/bbmetrics/static
> <Directory /var/www/bbmetrics/bbmetrics/static/>
> Order allow,deny
> Allow from all
> </Directory>
> ErrorLog /var/log/httpd/error.log
> LogLevel warn
> CustomLog /var/log/httpd/access.log combined
> </VirtualHost>
>
> Calling Python code for extracting variable. Is this the right way to extract
> environment variable for WSGI based Python Flask App after WSGI file is
> correctly written
>
> #!/usr/bin/python3
> import os
> import requests
> import datetime
> import configparser
>
> from flask import abort
>
>
> httpErrorCode = {
> 500: "server Error",
> 404: "Resource Not Found",
> 401: "Not Authorized"
> }
>
> deployEnv = os.environ['CE360AF_ENV']
>
> app.run() Python File
>
> #!/usr/bin/python3
> from flask import request
> from flask_restful import Resource, Api, reqparse
> from flask_cors import CORS
>
> from bbmetrics import app
> from bbmetrics.main import
> GetProjectRepos,GetAllPullRequestMetricsFromRepo,GetAllPullRequestMetricsFromRepoForDateRange
>
> api = Api(app)
> CORS(app)
>
>
> @app.route("/")
> def welcome():
> return "Welcome to CE360 Assessment Framework!!!"
>
> if __name__ == '__main__':
> app.run()
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "modwsgi" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/modwsgi/c3ffd820-917f-47cf-b19f-e03e76825443%40googlegroups.com
>
> <https://groups.google.com/d/msgid/modwsgi/c3ffd820-917f-47cf-b19f-e03e76825443%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
You received this message because you are subscribed to the Google Groups
"modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/modwsgi/6FA4347D-B62E-4340-B60E-645953B0DEF5%40gmail.com.