如何判断代码是否在Google App Engine上运行?

时间:2015-04-21 21:30:25

标签: google-app-engine

当我在本地运行google app引擎时,sys.platform报告linux3。这也意味着我也在linux平台上运行。

告诉我的代码是在谷歌应用引擎环境中运行的最终方式是什么?

1 个答案:

答案 0 :(得分:3)

一种方法是检查是否设置了APPENGINE_RUNTIME环境变量

在python中将是

import os
if 'APPENGINE_RUNTIME' in os.environ.keys():
    # working in appengine environment

这是一个特殊的appengine环境变量,没有为常规程序设置。

如果您对其他环境变量感兴趣,请按以下列表

INSTANCE_ID 7ae067a39bdd37e8ab1cf40c711a6dbca84d
wsgi.url_scheme http
wsgi.multiprocess True
wsgi.version (1, 0)
HTTP_USER_AGENT Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
REMOTE_ADDR 127.0.0.1
HTTP_X_APPENGINE_COUNTRY ZZ
SERVER_PROTOCOL HTTP/1.1
QUERY_STRING 
wsgi.run_once False
DATACENTER us1
REQUEST_METHOD GET
SERVER_SOFTWARE Development/2.0
HTTPS off
wsgi.multithread True
SCRIPT_NAME 
HTTP_ACCEPT */*
APPLICATION_ID dev~teledap-gae
SERVER_NAME localhost
USER_EMAIL 
REQUEST_LOG_ID 5e2e9ae098c4612cfdb1feebacea8dc5cb7e9e989355edfffcb4a30dbca2ecffa69b
wsgi.errors <google.appengine.api.logservice.logservice._LogsStreamBuffer object at 0x7fd47f9308d0>
PATH_INFO /
DEFAULT_VERSION_HOSTNAME localhost:8080
USER_NICKNAME 
APPENGINE_RUNTIME python27
CURRENT_VERSION_ID 1.790710791652471334
USER_ID 
USER_ORGANIZATION 
HTTP_CONTENT_LENGTH 0
HTTP_ACCEPT_LANGUAGE en-US,en;q=0.8,nl;q=0.6,es;q=0.4
HTTP_HOST localhost:8080
CURRENT_MODULE_ID default
TZ UTC
HTTP_COOKIE _ga=GA1.1.2122305447.1428161397; connect.sid=s%3AWErSIaaWXlGMkYqcwwBrikSFU1Xy4EGS.xOv8DCRhhTx2xaKiK4wRJsQh3ol9pG3njPxPexQiRdA
HTTP_CACHE_CONTROL no-cache
AUTH_DOMAIN gmail.com
USER_IS_ADMIN 0
wsgi.input <cStringIO.StringI object at 0x7fd47f92c0b8>
REQUEST_ID_HASH 9B7C4FEB
SERVER_PORT 8080

有些依赖于它们的运行位置或方式,例如: HTTP_USER_AGENT或REQUEST_METHOD可能会有所不同。

You may also define your own environmental variables如果您更愿意使用它们:

  

您可以在app.yaml中定义变量,使其可用于   程序的os.environ词典:

env_variables:
  DJANGO_SETTINGS_MODULE: 'myapp.settings'