App Engine 服务的出站 IP 地址

出站服务(例如,网址提取、套接字和 Mail API)使用大型 IP 地址池。此池中的 IP 地址范围可能会进行日常更改。实际上,来自同一应用的两个连续 API 调用可能看上去源于两个不同的 IP 地址。

您可以根据 Google 发布的 IP 范围信息查找 App Engine 服务的当前 IP 地址范围:

  • Google 在 goog.json 中发布了其向互联网用户提供的完整 IP 范围列表。

  • Google 还在 cloud.json 中发布了可用于客户的 Google Cloud 资源的全球和区域外部 IP 地址范围列表。

Google API 和服务使用的 IP 地址位于通过从 goog.json 的范围中删除 cloud.json 的所有范围之后计算得出的范围列表中。 以下示例显示了如何使用 Python 获取此范围。

您可以使用以下 Python 脚本创建 IP 地址范围列表,其中包含 Google API 和服务使用的 IP 地址范围。

如需了解如何运行此脚本,请参阅运行方式

from __future__ import print_function

import json

try:
    from urllib import urlopen
except ImportError:
    from urllib.request import urlopen
    from urllib.error import HTTPError

import netaddr

IPRANGE_URLS = {
    "goog": "https://www.gstatic.com/ipranges/goog.json",
    "cloud": "https://www.gstatic.com/ipranges/cloud.json",
}

def read_url(url):
    try:
        return json.loads(urlopen(url).read())
    except (IOError, HTTPError):
        print("ERROR: Invalid HTTP response from %s" % url)
    except json.decoder.JSONDecodeError:
        print("ERROR: Could not parse HTTP response from %s" % url)

def get_data(link):
    data = read_url(link)
    if data:
        print("{} published: {}".format(link, data.get("creationTime")))
        cidrs = netaddr.IPSet()
        for e in data["prefixes"]:
            if "ipv4Prefix" in e:
                cidrs.add(e.get("ipv4Prefix"))
            if "ipv6Prefix" in e:
                cidrs.add(e.get("ipv6Prefix"))
        return cidrs

def main():
    cidrs = {group: get_data(link) for group, link in IPRANGE_URLS.items()}
    if len(cidrs) != 2:
        raise ValueError("ERROR: Could process data from Google")
    print("IP ranges for Google APIs and services default domains:")
    for ip in (cidrs["goog"] - cidrs["cloud"]).iter_cidrs():
        print(ip)

if __name__ == "__main__":
    main()