如何在AppEngine中获得基本URI ?

时间:2023-01-24 18:17:32

How can I get the base URI in a Google AppEngine app written in Python? I'm using the webapp framework.

如何在用Python编写的谷歌AppEngine应用程序中获得基本URI ?我在使用webapp框架。

e.g.

如。

http://example.appspot.com/

2 个解决方案

#1


5  

The proper way to parse self.request.url is not with a regular expression, but with Python standard library's urlparse module:

解析self.request的正确方法。url不是用正则表达式,而是用Python标准库的urlparse模块:

import urlparse

...

o = urlparse.urlparse(self.request.url)

Object o will be an instance of the ParseResult class with string-valued fields such as o.scheme (probably http;-) and o.netloc ('example.appspot.com' in your case). You can put some of the strings back together again with the urlparse.urlunparse function from the same module, e.g.

对象o将是具有字符串值字段(如o)的ParseResult类的实例。scheme(可能是http;-)和o.netloc ('example.appspot.com'在你的例子中)。您可以使用urlparse将一些字符串重新组合在一起。来自同一模块的urlunparse函数,例如。

s = urlparse.urlunparse((o.scheme, o.netloc, '', '', '', ''))

which would give you in s the string 'http://example.appspot.com' in this case.

这里是字符串http://example.appspot.com。

#2


2  

If you just want to find your app ID, you can get that from the environment without having to parse the current URL. The environment variable is APPLICATION_ID

如果您只想找到应用程序ID,您可以从环境中获取它,而不必解析当前URL。环境变量是APPLICATION_ID

You can also use this to find the current version (CURRENT_VERSION_ID), auth domain (which will let you know whether you're running on appspot.com, AUTH_DOMAIN) and whether you're running on the local development server or in production (SERVER_SOFTWARE).

您还可以使用它来查找当前版本(CURRENT_VERSION_ID)、auth域(它将让您知道您是在appspot.com、AUTH_DOMAIN上运行),以及您是在本地开发服务器还是在产品(SERVER_SOFTWARE)上运行。

So to get the full base URL, try something like this:

要获得完整的基本URL,可以尝试以下方法:

import os

def get_base_url():
    if os.environ[AUTH_DOMAIN] == "gmail.com":
        app_id = os.environ[APPLICATION_ID]
        return "http://" + app_id + ".appspot.com"
    else:
        return "http://" + os.environ[AUTH_DOMAIN]

edit: AUTH_DOMAIN contains the custom domain, no need to include the app ID.

编辑:AUTH_DOMAIN包含自定义域,不需要包含app ID。

This will return the current version's base URL even if you're not accessing the current version, or if you visit the current version using a URL like http://current-version.latest.app_id.appspot.com (unlike URL-parsing methods)

这将返回当前版本的基本URL,即使您没有访问当前版本,或者如果您使用http://currentversion.latest.app_id.appspot.com之类的URL访问当前版本(与URL解析方法不同)

#1


5  

The proper way to parse self.request.url is not with a regular expression, but with Python standard library's urlparse module:

解析self.request的正确方法。url不是用正则表达式,而是用Python标准库的urlparse模块:

import urlparse

...

o = urlparse.urlparse(self.request.url)

Object o will be an instance of the ParseResult class with string-valued fields such as o.scheme (probably http;-) and o.netloc ('example.appspot.com' in your case). You can put some of the strings back together again with the urlparse.urlunparse function from the same module, e.g.

对象o将是具有字符串值字段(如o)的ParseResult类的实例。scheme(可能是http;-)和o.netloc ('example.appspot.com'在你的例子中)。您可以使用urlparse将一些字符串重新组合在一起。来自同一模块的urlunparse函数,例如。

s = urlparse.urlunparse((o.scheme, o.netloc, '', '', '', ''))

which would give you in s the string 'http://example.appspot.com' in this case.

这里是字符串http://example.appspot.com。

#2


2  

If you just want to find your app ID, you can get that from the environment without having to parse the current URL. The environment variable is APPLICATION_ID

如果您只想找到应用程序ID,您可以从环境中获取它,而不必解析当前URL。环境变量是APPLICATION_ID

You can also use this to find the current version (CURRENT_VERSION_ID), auth domain (which will let you know whether you're running on appspot.com, AUTH_DOMAIN) and whether you're running on the local development server or in production (SERVER_SOFTWARE).

您还可以使用它来查找当前版本(CURRENT_VERSION_ID)、auth域(它将让您知道您是在appspot.com、AUTH_DOMAIN上运行),以及您是在本地开发服务器还是在产品(SERVER_SOFTWARE)上运行。

So to get the full base URL, try something like this:

要获得完整的基本URL,可以尝试以下方法:

import os

def get_base_url():
    if os.environ[AUTH_DOMAIN] == "gmail.com":
        app_id = os.environ[APPLICATION_ID]
        return "http://" + app_id + ".appspot.com"
    else:
        return "http://" + os.environ[AUTH_DOMAIN]

edit: AUTH_DOMAIN contains the custom domain, no need to include the app ID.

编辑:AUTH_DOMAIN包含自定义域,不需要包含app ID。

This will return the current version's base URL even if you're not accessing the current version, or if you visit the current version using a URL like http://current-version.latest.app_id.appspot.com (unlike URL-parsing methods)

这将返回当前版本的基本URL,即使您没有访问当前版本,或者如果您使用http://currentversion.latest.app_id.appspot.com之类的URL访问当前版本(与URL解析方法不同)