[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r3764: mod_python-ize the geoip.py script. (vidalia/trunk/contrib)



Author: edmanm
Date: 2009-05-07 18:43:13 -0400 (Thu, 07 May 2009)
New Revision: 3764

Modified:
   vidalia/trunk/contrib/geoip.py
Log:

mod_python-ize the geoip.py script.


Modified: vidalia/trunk/contrib/geoip.py
===================================================================
--- vidalia/trunk/contrib/geoip.py	2009-05-07 03:20:28 UTC (rev 3763)
+++ vidalia/trunk/contrib/geoip.py	2009-05-07 22:43:13 UTC (rev 3764)
@@ -11,67 +11,69 @@
 ##  the terms described in the LICENSE file.
 ##
 
+from mod_python import apache
+from mod_python import util
+import GeoIP
 import os
-import cgi
-import GeoIP
 
-
 def escape_output(f):
   f.replace("\\", "\\\\")
   f.replace("\"", "\\\"")
   return ("\"%s\"" % f)
 
-
 # Get the form fields
-form = cgi.FieldStorage()
+def handler(req):
+  form = util.FieldStorage(req)
 
-# 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 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'])
+  # 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(req.connection.remote_ip)
 
+  # Open the GeoIP database
+  gi = GeoIP.open("/var/lib/geoip/GeoLiteCity.dat", GeoIP.GEOIP_MEMORY_CACHE)
 
-# Open the GeoIP database
-gi = GeoIP.open("/var/lib/geoip/GeoLiteCity.dat", GeoIP.GEOIP_MEMORY_CACHE)
+  # Display the output according to the specified format
+  req.content_type = 'text/plain'
 
-# 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'])
 
-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])
+        
+        response = " ".join(fields)
+        req.write(response)
 
-      r = gi.range_by_ip(ip)
-      if len(r) == 2:
-        fields.append("FROM=%s" % r[0])
-        fields.append("TO=%s" % r[1])
+  elif format == "short":
+    for ip in iplist:
+      r = gi.record_by_addr(ip)
+      if r != None:
+        response = "%s,%s,%s,%s,%f,%f" % (ip, r['city'].replace(",", " "), r['region'],
+                                          r['country_code'],r['latitude'],r['longitude'])
+        req.write(response)
 
-      print " ".join(fields)
+  return apache.OK
 
-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'])
-