Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit c0ee78c

Browse files
author
tjmather
committed
added support for GeoIP City
1 parent 00b6bc6 commit c0ee78c

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

ChangeLog

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
1.1.0
1+
1.2.0 2003-05-08
2+
* Added support for GeoIP City
3+
4+
1.1.0 2003-03-18
25
* Added support for GeoIP ISP and Organization
36

47
0.2.0 2002-06-25

README

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ To Install:
66
python setup.py install
77

88
Usage:
9-
See test.py for example usage
9+
See test.py for example usage with GeoIP Country
10+
See test_org.py for example usage with GeoIP ISP and Organization
11+
See test_city.py for example usage with GeoIP City
1012

1113
License:
1214

13-
Copyright (c) 2003 MaxMind.com.
15+
Copyright (c) 2003 MaxMind LLC
1416

1517
All rights reserved. This package is free software; it is licensed
1618
under the GPL.

py_GeoIP.c

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
22
/* py_GeoIP.c
33
*
4-
* Copyright (C) 2002 MaxMind.com
4+
* Copyright (C) 2003 MaxMind LLC
55
*
66
* This library is free software; you can redistribute it and/or
77
* modify it under the terms of the GNU General Public
@@ -20,6 +20,7 @@
2020

2121
#include <Python.h>
2222
#include "GeoIP.h"
23+
#include "GeoIPCity.h"
2324

2425
staticforward PyTypeObject GeoIP_GeoIPType;
2526

@@ -143,13 +144,41 @@ static PyObject * GeoIP_org_by_name_Py(PyObject *self, PyObject *args) {
143144
return Py_BuildValue("s", retval);
144145
}
145146

147+
static PyObject * GeoIP_populate_dict(GeoIPRecord *gir) {
148+
PyObject * retval;
149+
retval = PyDict_New();
150+
PyDict_SetItem(retval,Py_BuildValue("s","country_code"),Py_BuildValue("s",gir->country_code));
151+
PyDict_SetItem(retval,Py_BuildValue("s","country_code3"),Py_BuildValue("s",gir->country_code3));
152+
PyDict_SetItem(retval,Py_BuildValue("s","country_name"),Py_BuildValue("s",gir->country_name));
153+
PyDict_SetItem(retval,Py_BuildValue("s","region"),Py_BuildValue("s",gir->region));
154+
PyDict_SetItem(retval,Py_BuildValue("s","city"),Py_BuildValue("s",gir->city));
155+
PyDict_SetItem(retval,Py_BuildValue("s","postal_code"),Py_BuildValue("s",gir->postal_code));
156+
PyDict_SetItem(retval,Py_BuildValue("s","latitude"),Py_BuildValue("f",gir->latitude));
157+
PyDict_SetItem(retval,Py_BuildValue("s","longitude"),Py_BuildValue("f",gir->longitude));
158+
PyDict_SetItem(retval,Py_BuildValue("s","dma_code"),Py_BuildValue("i",gir->dma_code));
159+
PyDict_SetItem(retval,Py_BuildValue("s","area_code"),Py_BuildValue("i",gir->area_code));
160+
return retval;
161+
}
162+
163+
static PyObject * GeoIP_record_by_addr_Py(PyObject *self, PyObject *args) {
164+
char * addr;
165+
GeoIPRecord * gir;
166+
GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
167+
if (!PyArg_ParseTuple(args, "s", &addr)) {
168+
return NULL;
169+
}
170+
gir = GeoIP_record_by_addr(GeoIP->gi, addr);
171+
return GeoIP_populate_dict(gir);
172+
}
173+
146174
static PyMethodDef GeoIP_Object_methods[] = {
147175
{"country_code_by_name", GeoIP_country_code_by_name_Py, 1, "Lookup Country Code By Name"},
148176
{"country_name_by_name", GeoIP_country_name_by_name_Py, 1, "Lookup Country Name By Name"},
149177
{"country_code_by_addr", GeoIP_country_code_by_addr_Py, 1, "Lookup Country Code By IP Address"},
150178
{"country_name_by_addr", GeoIP_country_name_by_addr_Py, 1, "Lookup Country Name By IP Address"},
151179
{"org_by_addr", GeoIP_org_by_addr_Py, 1, "Lookup Organization or ISP By IP Address"},
152180
{"org_by_name", GeoIP_org_by_name_Py, 1, "Lookup Organization or ISP By Name"},
181+
{"record_by_addr", GeoIP_record_by_addr_Py, 1, "Lookup City Region By IP Address"},
153182
{NULL, NULL, 0, NULL}
154183
};
155184

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
sources = ['py_GeoIP.c'])
66

77
setup (name = 'GeoIP-Python',
8-
version = '0.2.0',
8+
version = '1.2.0',
99
description = 'This is a python wrapper to GeoIP',
1010
ext_modules = [module1])

test_city.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
3+
import GeoIP
4+
import GeoIPRecord
5+
6+
gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD)
7+
8+
gir = gi.record_by_addr("24.24.24.24")
9+
10+
print gir['country_code'] + '\t' + gir['country_code3'] + '\t' + gir['country_name'] + '\t' + gir['region'] + '\t' + gir['city'] + '\t' + gir['postal_code']
11+
print gir['latitude']
12+
print gir['longitude']
13+
print gir['dma_code']
14+
print gir['area_code'];

0 commit comments

Comments
 (0)