开发者

How can my code tell if it's running on Google's server or my local development server? [duplicate]

开发者 https://www.devze.com 2023-02-06 04:38 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: In Python, how can I test if I'm in Google App Engine SDK?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

In Python, how can I test if I'm in Google App Engine SDK?

Is there an environment flag that will tell if my code is running in production or on the development server?

I'd like to be able to use an if statement to do something different depending on this flag.

(Specific case: newlines in a text file I want to read are treated different开发者_高级运维ly in the two environments.)


if os.environ.get('SERVER_SOFTWARE','').startswith('Development'):
    DEBUG = True
    logging.debug("[*] Debug info activated")


For Google App Engine, since I've been told that you can't import socket you could probably use that fact to determine if you're running on GAE. This solution isn't 100% foolproof, but it should do what you want. Put this in settings.py.

try:
    import socket
except ImportError:
    DEBUG = TRUE

For anyone using Django but not GAE, you can use this instead. Change 'devserver' to whatever the hostname of your development server is.

import socket
if socket.gethostname() == 'devserver':
    DEBUG = TRUE

Then wherever you need to check the DEBUG variable in your code

from django.conf import settings

if settings.DEBUG:
    newline = '\n'
0

精彩评论

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