Date created: 03/12/21 09:07:58. Last modified: 03/12/21 09:08:46

CGP Cloud Functions Examples

Examples of GCP Cloud Functions which return the caller's IP either via JSON, XML or as plain text/raw.

ip-json.py:

from flask import jsonify, make_response

def main(request):
return make_response(
jsonify(
{
"ip": request.environ['HTTP_X_FORWARDED_FOR'],
}
),
200
)

ip-raw.py:

from flask import make_response

def main(request):
return make_response(
request.environ['HTTP_X_FORWARDED_FOR'],
200
)

ip-xml.py:

from flask import make_response

def main(request):
resp = make_response(
f"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
f"<ip>{request.environ['HTTP_X_FORWARDED_FOR']}</ipv4>"
)
resp.headers['Content-Type'] = 'application/xml'
return resp