开发者

How do I know if my code is running deployed on GAE or running local?

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 condi开发者_如何学Gotionally perform this import or other local-specific stuff?


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.


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'):


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']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜