[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[vidalia-svn] r3759: Add a Python GeoIP CGI script that supports both the old ("s (vidalia/trunk/contrib)
Author: edmanm
Date: 2009-05-05 23:31:57 -0400 (Tue, 05 May 2009)
New Revision: 3759
Added:
vidalia/trunk/contrib/geoip.py
Log:
Add a Python GeoIP CGI script that supports both the old ("short")
and the new ("long") output formats.
Added: vidalia/trunk/contrib/geoip.py
===================================================================
--- vidalia/trunk/contrib/geoip.py (rev 0)
+++ vidalia/trunk/contrib/geoip.py 2009-05-06 03:31:57 UTC (rev 3759)
@@ -0,0 +1,77 @@
+#!/usr/bin/python
+##
+## $Id$
+##
+## This file is part of Vidalia, and is subject to the license terms in the
+## LICENSE file, found in the top level directory of this distribution. If
+## you did not receive the LICENSE file with this file, you may obtain it
+## from the Vidalia source package distributed by the Vidalia Project at
+## http://www.vidalia-project.net/. No part of Vidalia, including this file,
+## may be copied, modified, propagated, or distributed except according to
+## the terms described in the LICENSE file.
+##
+
+import os
+import cgi
+import GeoIP
+
+
+def escape_output(f):
+ f.replace("\\", "\\\\")
+ f.replace("\"", "\\\"")
+ return ("\"%s\"" % f)
+
+
+# Get the form fields
+form = cgi.FieldStorage()
+
+# Get the specified output format and verify it is a valid format. If
+# none is specified, default to the old "short" output format.
+format = form.getfirst("format", "short").lower()
+if format not in ("short", "long"):
+ format = "short"
+
+# Get a list of all IP addresses in this request. If none is specified,
+# display the GeoIP information for the client's address.
+iplist = []
+if form.has_key("ip"):
+ for ip in form.getlist("ip"):
+ iplist.extend(ip.split(","))
+else:
+ iplist.append(os.environ['REMOTE_ADDR'])
+
+
+# Open the GeoIP database
+gi = GeoIP.open("/var/lib/geoip/GeoLiteCity.dat", GeoIP.GEOIP_MEMORY_CACHE)
+
+# Display the output according to the specified format
+print "Content-Type: text/plain"
+print
+
+if format == "long":
+ for ip in iplist:
+ r = gi.record_by_addr(ip)
+ if r != None:
+ fields = []
+ fields.append("IP=%s" % ip)
+ fields.append("LAT=%f" % r['latitude'])
+ fields.append("LON=%f" % r['longitude'])
+ fields.append("CITY=%s" % escape_output(r['city']))
+ fields.append("REGION=%s" % escape_output(r['region_name']))
+ fields.append("COUNTRY=%s" % escape_output(r['country_name']))
+ fields.append("CC=%s" % r['country_code'])
+
+ r = gi.range_by_ip(ip)
+ if len(r) == 2:
+ fields.append("FROM=%s" % r[0])
+ fields.append("TO=%s" % r[1])
+
+ print " ".join(fields)
+
+elif format == "short":
+ for ip in iplist:
+ r = gi.record_by_addr(ip)
+ if r != None:
+ print "%s,%s,%s,%s,%f,%f" % (ip, r['city'].replace(",", " "), r['region'],
+ r['country_code'],r['latitude'],r['longitude'])
+
Property changes on: vidalia/trunk/contrib/geoip.py
___________________________________________________________________
Added: svn:executable
+ *
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native