How do I know if my code is running deployed on GA

2019-08-15 16:53发布

问题:

I have a few imports and statements needed to get some crypto modules from gdata to load into my GAE Python SDK:

from google.appengine.tools.dev_appserver import HardenedModulesHook
HardenedModulesHook._WHITE_LIST_C_MODULES += ['_counter']

But this import does not work (nor is it needed) while deployed on GAE, only local.

How can I test if the code is running on GAE or local so I can conditionally perform this import or other local-specific stuff?

回答1:

If the import actually doesn't work because it throws an ImportError then your best choice it to just try/except the error.

try:
    from google.appengine.tools.dev_appserver import HardenedModulesHook
    HardenedModulesHook._WHITE_LIST_C_MODULES += ['_counter']
except ImportError:
    HardenedModulesHook = None

You could just pass in the except block but doing it this way lets you check the HardenedModulesHook reference and perform some application logic.



回答2:

I use this on some of my pet projects. Can't remember where I got it.

import os
if 'SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'].startswith('Dev'):


回答3:

You said the import doesn't work while deployed on GAE, so why not simply do something like this?

try:
    from google.appengine.tools.dev_appserver import HardenedModulesHook

HardenedModulesHook._WHITE_LIST_C_MODULES += ['_counter']