1

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?

  • possible duplicate of [In Python, how can I test if I'm in Google App Engine SDK?](http://stackoverflow.com/questions/1916579/in-python-how-can-i-test-if-im-in-google-app-engine-sdk) – Wooble Jun 21 '11 at 20:24
  • possible duplicate of [How to determine if your app is running on local Python Development Server?](http://stackoverflow.com/questions/5914802/how-to-determine-if-your-app-is-running-on-local-python-development-server) – anthony Jun 21 '11 at 20:25
  • 2
    I'm puzzled - why do you want/need to do this only on the dev_appserver? What do you do in production in the absence of these C modules? – Nick Johnson Jun 22 '11 at 00:22
  • The gdata.tlslite.util crypto modules uses some M2Crypto modules when they are around, and chooses openssl as it's preferred way to make keys. When on GAE, the openssl is not an option, so it doesn't try, it used the pure Python implementation. On my SDK, it finds the M2Crypto and tries to import it, even though I specify python implementation of keys. There are perhaps better ways to exclude the M2Crypto from my GAE SDK, but this works. I like having M2Crypto around so I can more easily check that my keys and signatures, etc are portable. – Joseph Santaniello Jun 22 '11 at 09:38

3 Answers3

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'):
Mathias Nielsen
  • 1,560
  • 1
  • 17
  • 31
2

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.

Chris W.
  • 37,583
  • 36
  • 99
  • 136
0

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']
JAB
  • 20,783
  • 6
  • 71
  • 80