GAE:检查我是否在dev_appserver上或已部署到appspot的python代码 [英] GAE: python code to check if i'm on dev_appserver or deployed to appspot
问题描述
我的GAE应用程序使用OAUTH2.目前,我是根据是使用dev_appserver
进行本地测试还是将其部署到appspot.com,在http://localhost:8081/oauth2callback
和myapp.appspot.com/oauth2callback
之间手动切换redirect_uri
.
My GAE app uses OAUTH 2. Currently, I manually switch the redirect_uri
between http://localhost:8081/oauth2callback
and myapp.appspot.com/oauth2callback
depending on whether do local testing with dev_appserver
or if I deploy to appspot.com.
是否可以使用一些代码(例如,使用os.environ
)来自动执行此操作?
Is there some code (e.g. using os.environ
) that I can use to automate this?
谢谢!
推荐答案
您可以通过os.environ['SERVER_NAME']
获取当前域名.
You can get you current domain name with os.environ['SERVER_NAME']
.
您还可以按版本名称进行检查:
You also can check this by version name:
import os
if os.environ['APPLICATION_ID'].startswith('dev'):
pass # it's localhost
else:
pass # it's uploaded on gae
如果使用webapp2,最好使用self.uri_for('callback-uri-name', _full=True)
构建回调uri.
Will be better to use self.uri_for('callback-uri-name', _full=True)
for building callback uri if you use webapp2.
您的路线中有类似的东西.
You have something like this in your routes.
app = webapp2.WSGIApplication([
webapp2.Route(r'/oauth/callback', handler='oauth.CallbackHandler', name='callback-uri-name'),
])
然后,如果您拨打self.uri_for('callback-uri-name', _full=True)
,您会得到类似http://localhost:8080/oauth/callback
或http://app-id.appspot.com/oauth/callback
的信息.
Then, if you'll call self.uri_for('callback-uri-name', _full=True)
you'll get something like http://localhost:8080/oauth/callback
or http://app-id.appspot.com/oauth/callback
.
有关更多信息: http://webapp-improved.appspot.com/guide/routing .html#building-uris
这篇关于GAE:检查我是否在dev_appserver上或已部署到appspot的python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!