开发者

how to get DomainName where a appengine/python website is hosted

I have a appengine webapp where i need to set HTTP Location variable to redirect to ano开发者_C百科ther page in the same webapp. For that i need to produce a absolute link. for portability reason i cannot use directly the domain name which i am currently using.

Is it possible to produce the domain name on which the webapp is hosted in the python code.


If you're using the webapp framework, the current URL is in self.request.url. Various components are broken out in properties of self.request, documented here.


I don't think I quite fully understand the need to getdomain name. But check out if redirect API provided by google app engine will do the job. http://code.google.com/appengine/docs/python/tools/webapp/redirects.html


this question is poorly answered. You need the domain name to generate pages like: robots.txt, sitemap.xml and many many more things which are not relative links. Ive tried using this:

 from google.appengine.api.app_identity import get_default_version_hostname
 host = get_default_version_hostname()

but... it is not good when you upgrade to your own domain... because I still get the appspot.com name.


If your application uses a custom domain name, then get_default_version_hostname() will return myapp.appspot.com, or 1.default.myapp.appspot.com not mydomain.com

To get the domain name in the request, use:

hostname = self.request.host 

According to the webob docs:

returns 'Host name provided in HTTP_HOST, with fall-back to SERVER_NAME'

This won't work inside a cron task or if the client calls myapp.appspot.com instead of mydomain.com.

See also issue: https://code.google.com/p/googleappengine/issues/detail?id=11993 get_default_version_hostname() won't work inside /_ah/start handler on a Managed VM. the workaround is to set: os.environ['DEFAULT_VERSION_HOSTNAME'] = os.environ['GAE_APPENGINE_HOSTNAME']

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜