-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
44 lines (32 loc) · 1.15 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/', methods=['GET'])
def root():
# read headers
headers = request.headers
country = headers.get('X-AppEngine-Country', '')
# if the country header is missing, or is ZZ
# then app engine doesn't have geoip data
if not country or country == 'ZZ':
# return an empty object
return jsonify({})
region = headers.get('X-AppEngine-Region', '')
city = headers.get('X-AppEngine-City', '')
lat_long = headers.get('X-AppEngine-CityLatLong', ',').split(',')
latitude = lat_long[0]
longitude = lat_long[1]
# Requests are proxid in App engine so request.remote_addr will not work
# We have this special header to read the original ip address
x_forwarded_for = request.headers.get('X-Forwarded-For')
ip_address = x_forwarded_for.split(',')[0]
geoip_data = {
'ipAddress': ip_address,
'countryCode': country,
'regionCode': region,
'city': city,
'latitude': latitude,
'longitude': longitude,
}
return jsonify(geoip_data)
if __name__ == '__main__':
app.run(debug=True)