How to get current domain name with Python/GAE?
Current url is
http://myapp.appspot.com/something/<user-id>
or
http://127.0.0.1:8080/something/<user-id>
How in my python code I can get http://myapp.appspot.com/
or http://127.0.0.1:8080/
?
This is need for dynamic links generation, for ex., to http://myapp.appspo开发者_Go百科t.com/somethingelse
.
self.request.path
returns the whole path.
self.request.host_url
I think you want app_identity.get_default_version_hostname().
If an app is served from a custom domain, it may be necessary to retrieve the entire hostname component. You can do this using the app_identity.get_default_version_hostname() method.
This code:
logging.info(app_identity.get_default_version_hostname())
prints localhost:8080
on the development server.
If self.request.path
returns the whole path, can't you just do:
import urlparse
def get_domain(url):
return urlparse.urlparse(url).netloc
>>> get_domain("http://myapp.appspot.com/something/") 'myapp.appspot.com'
精彩评论