Date created: Friday, March 12, 2021 9:07:58 AM. Last modified: Tuesday, January 16, 2024 3:31:51 PM

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

Previous page: Bit Operations
Next page: Check Multicast MAC