From c1bed5e34ad718e51d671beb959ae7f593499ef6 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Mon, 16 Feb 2015 21:39:51 -0600 Subject: [PATCH] Output requester's IP in reply --- application.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/application.py b/application.py index 8f641a1..227256f 100644 --- a/application.py +++ b/application.py @@ -33,6 +33,17 @@ def isFloat(s): return 0 return 1 +def IP2Int(ip): + o = map(int, ip.split('.')) + res = (16777216 * o[0]) + (65536 * o[1]) + (256 * o[2]) + o[3] + return res + +def getClientAddress(environ): + try: + return environ['HTTP_X_FORWARDED_FOR'].split(',')[-1].strip() + except KeyError: + return environ['REMOTE_ADDR'] + def application(environ, start_response): path = environ.get('PATH_INFO') parameters = cgi.parse_qs(environ.get('QUERY_STRING', '')) @@ -57,6 +68,8 @@ def application(environ, start_response): maxh, minh, meant, pre, pre_today, h_today, sunrise, sunset, scale, toffset = [-1, -1, -500, -1, -1, -1, -1, -1, -1, -1] + eip = IP2Int(getClientAddress(environ)) + # if loc is GPS coordinate itself sp = loc.split(',', 1) if len(sp)==2 and isFloat(sp[0]) and isFloat(sp[1]): @@ -212,9 +225,9 @@ def application(environ, start_response): sunset = int(((sunset +delta)%86400)/60) if of=='json' or of=='JSON': - output = '{"scale":%d, "tz":%d, "sunrise":%d, "sunset":%d, "maxh":%d, "minh":%d, "meant":%d, "pre":%f, "prec":%f, "hc":%d}' % (scale, toffset, sunrise, sunset, int(maxh), int(minh), int(meant), pre, pre_today, int(h_today)) + output = '{"scale":%d, "tz":%d, "sunrise":%d, "sunset":%d, "maxh":%d, "minh":%d, "meant":%d, "pre":%f, "prec":%f, "hc":%d, "eip":%d}' % (scale, toffset, sunrise, sunset, int(maxh), int(minh), int(meant), pre, pre_today, int(h_today), eip) else: - output = '&scale=%d&tz=%d&sunrise=%d&sunset=%d&maxh=%d&minh=%d&meant=%d&pre=%f&prec=%f&hc=%d' % (scale, toffset, sunrise, sunset, int(maxh), int(minh), int(meant), pre, pre_today, int(h_today)) + output = '&scale=%d&tz=%d&sunrise=%d&sunset=%d&maxh=%d&minh=%d&meant=%d&pre=%f&prec=%f&hc=%d&eip=%d' % (scale, toffset, sunrise, sunset, int(maxh), int(minh), int(meant), pre, pre_today, int(h_today), eip) response_headers = [('Content-type', 'text/plain'),('Content-Length', str(len(output)))] start_response(status, response_headers)