Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pygeoip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pygeoip
Commits
f551dfb5
Commit
f551dfb5
authored
Jul 22, 2013
by
William Tisäter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename region_name to region_code
parent
e957f76c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
33 deletions
+34
-33
README.md
+9
-7
pygeoip/__init__.py
+20
-21
tests/test_city.py
+4
-4
tests/test_region.py
+1
-1
No files found.
README.md
View file @
f551dfb5
...
...
@@ -57,21 +57,23 @@ Create your GeoIP instance with appropriate access flag. `STANDARD` reads data f
>>> gi = pygeoip.GeoIP('/path/to/GeoIPRegion.dat')
>>> gi.region_by_name('apple.com')
{'region_
nam
e': 'CA', 'country_code': 'US'}
{'region_
cod
e': 'CA', 'country_code': 'US'}
### City Lookup ###
>>> gi = pygeoip.GeoIP('/path/to/GeoIPCity.dat')
>>> gi.record_by_addr('64.233.161.99')
{
'city': 'Mountain View',
'region_
name':
'CA',
'city':
u
'Mountain View',
'region_
code': u
'CA',
'area_code': 650,
'longitude': -122.0574,
'country_code3': 'USA',
'latitude': 37.419199999999989,
'postal_code': '94043',
'time_zone': 'America/Los_Angeles',
'dma_code': 807,
'metro_code': 'San Francisco, CA',
'country_code3': 'USA',
'latitude': 37.41919999999999,
'postal_code': u'94043',
'longitude': -122.0574,
'country_code': 'US',
'country_name': 'United States',
'continent': 'NA'
...
...
pygeoip/__init__.py
View file @
f551dfb5
...
...
@@ -288,19 +288,18 @@ class GeoIP(object):
def
_get_region
(
self
,
ipnum
):
"""
Seek and return the region info (dict containing country_code
and region_name).
Seek and return the region information.
@param ipnum: Converted IP address
@type ipnum: int
@return: dict containing country_code and region_
nam
e
@return: dict containing country_code and region_
cod
e
@rtype: dict
"""
region
=
''
country_code
=
''
region
_code
=
None
country_code
=
None
seek_country
=
self
.
_seek_country
(
ipnum
)
def
get_region_
nam
e
(
offset
):
def
get_region_
cod
e
(
offset
):
region1
=
chr
(
offset
//
26
+
65
)
region2
=
chr
(
offset
%
26
+
65
)
return
''
.
join
([
region1
,
region2
])
...
...
@@ -309,7 +308,7 @@ class GeoIP(object):
seek_region
=
seek_country
-
const
.
STATE_BEGIN_REV0
if
seek_region
>=
1000
:
country_code
=
'US'
region
=
get_region_nam
e
(
seek_region
-
1000
)
region
_code
=
get_region_cod
e
(
seek_region
-
1000
)
else
:
country_code
=
const
.
COUNTRY_CODES
[
seek_region
]
elif
self
.
_databaseType
==
const
.
REGION_EDITION_REV1
:
...
...
@@ -318,20 +317,20 @@ class GeoIP(object):
pass
elif
seek_region
<
const
.
CANADA_OFFSET
:
country_code
=
'US'
region
=
get_region_nam
e
(
seek_region
-
const
.
US_OFFSET
)
region
_code
=
get_region_cod
e
(
seek_region
-
const
.
US_OFFSET
)
elif
seek_region
<
const
.
WORLD_OFFSET
:
country_code
=
'CA'
region
=
get_region_nam
e
(
seek_region
-
const
.
CANADA_OFFSET
)
region
_code
=
get_region_cod
e
(
seek_region
-
const
.
CANADA_OFFSET
)
else
:
index
=
(
seek_region
-
const
.
WORLD_OFFSET
)
//
const
.
FIPS_RANGE
if
index
in
const
.
COUNTRY_CODES
:
country_code
=
const
.
COUNTRY_CODES
[
index
]
elif
self
.
_databaseType
in
const
.
CITY_EDITIONS
:
rec
=
self
.
_get_record
(
ipnum
)
region
=
rec
.
get
(
'region_name'
,
'
'
)
country_code
=
rec
.
get
(
'country_code'
,
''
)
region
_code
=
rec
.
get
(
'region_code
'
)
country_code
=
rec
.
get
(
'country_code'
)
return
{
'country_code'
:
country_code
,
'region_
name'
:
region
}
return
{
'country_code'
:
country_code
,
'region_
code'
:
region_code
}
def
_get_record
(
self
,
ipnum
):
"""
...
...
@@ -339,9 +338,9 @@ class GeoIP(object):
@param ipnum: Converted IP address
@type ipnum: int
@return: dict with c
ountry_code, country_code3, country_nam
e,
region, city, postal_code, latitude, longitu
de,
dma_code, metro_code, area_code, region_name, time_zone
@return: dict with c
ity, region_code, area_code, time_zon
e,
dma_code, metro_code, country_code3, latitude, postal_co
de,
longitude, country_code, country_name, continent
@rtype: dict
"""
seek_country
=
self
.
_seek_country
(
ipnum
)
...
...
@@ -387,7 +386,7 @@ class GeoIP(object):
return
offset
,
buf
[
buf_pos
:
offset
]
return
offset
,
''
offset
,
record
[
'region_
nam
e'
]
=
get_data
(
buf
,
buf_pos
)
offset
,
record
[
'region_
cod
e'
]
=
get_data
(
buf
,
buf_pos
)
offset
,
record
[
'city'
]
=
get_data
(
buf
,
offset
+
1
)
offset
,
record
[
'postal_code'
]
=
get_data
(
buf
,
offset
+
1
)
buf_pos
=
offset
+
1
...
...
@@ -417,7 +416,7 @@ class GeoIP(object):
record
[
'area_code'
]
=
dmaarea_combo
%
1000
record
[
'metro_code'
]
=
const
.
DMA_MAP
.
get
(
record
[
'dma_code'
])
params
=
(
record
[
'country_code'
],
record
[
'region_
nam
e'
])
params
=
(
record
[
'country_code'
],
record
[
'region_
cod
e'
])
record
[
'time_zone'
]
=
time_zone_by_country_and_region
(
*
params
)
return
record
...
...
@@ -562,7 +561,7 @@ class GeoIP(object):
@type addr: str
@return: Dictionary with country_code, country_code3, country_name,
region, city, postal_code, latitude, longitude, dma_code,
metro_code, area_code, region_
nam
e, time_zone
metro_code, area_code, region_
cod
e, time_zone
@rtype: dict
"""
if
self
.
_databaseType
not
in
const
.
CITY_EDITIONS
:
...
...
@@ -585,7 +584,7 @@ class GeoIP(object):
@type hostname: str
@return: Dictionary with country_code, country_code3, country_name,
region, city, postal_code, latitude, longitude, dma_code,
metro_code, area_code, region_
nam
e, time_zone
metro_code, area_code, region_
cod
e, time_zone
@rtype: dict
"""
addr
=
self
.
_gethostbyname
(
hostname
)
...
...
@@ -598,7 +597,7 @@ class GeoIP(object):
@param addr: IP address
@type addr: str
@return: Dictionary containing country_code
, region and region_nam
e
@return: Dictionary containing country_code
and region_cod
e
@rtype: dict
"""
if
self
.
_databaseType
not
in
const
.
REGION_CITY_EDITIONS
:
...
...
@@ -615,7 +614,7 @@ class GeoIP(object):
@param hostname: Hostname
@type hostname: str
@return: Dictionary containing country_code, region
, and region_name
@return: Dictionary containing country_code, region
_code and region
@rtype: dict
"""
addr
=
self
.
_gethostbyname
(
hostname
)
...
...
tests/test_city.py
View file @
f551dfb5
...
...
@@ -21,7 +21,7 @@ class TestGeoIPCityFunctions(unittest.TestCase):
self
.
us_record_data
=
{
'city'
:
'Mountain View'
,
'region_
nam
e'
:
'CA'
,
'region_
cod
e'
:
'CA'
,
'area_code'
:
650
,
'longitude'
:
-
122.05740356445312
,
'country_code3'
:
'USA'
,
...
...
@@ -36,7 +36,7 @@ class TestGeoIPCityFunctions(unittest.TestCase):
self
.
gb_record_data
=
{
'city'
:
'Tadworth'
,
'region_
nam
e'
:
'N7'
,
'region_
cod
e'
:
'N7'
,
'area_code'
:
0
,
'longitude'
:
-
0.23339999999998895
,
'country_code3'
:
'GBR'
,
...
...
@@ -49,8 +49,8 @@ class TestGeoIPCityFunctions(unittest.TestCase):
'time_zone'
:
'Europe/London'
}
self
.
us_region_data
=
{
'region_
nam
e'
:
'CA'
,
'country_code'
:
'US'
}
self
.
gb_region_data
=
{
'region_
nam
e'
:
'N7'
,
'country_code'
:
'GB'
}
self
.
us_region_data
=
{
'region_
cod
e'
:
'CA'
,
'country_code'
:
'US'
}
self
.
gb_region_data
=
{
'region_
cod
e'
:
'N7'
,
'country_code'
:
'GB'
}
self
.
gic
=
pygeoip
.
GeoIP
(
CITY_DB_PATH
)
self
.
gic_mem
=
pygeoip
.
GeoIP
(
CITY_DB_PATH
,
pygeoip
.
MEMORY_CACHE
)
...
...
tests/test_region.py
View file @
f551dfb5
...
...
@@ -11,7 +11,7 @@ class TestGeoIPRegionFunctions(unittest.TestCase):
self
.
us_hostname
=
'apple.com'
self
.
us_ip
=
'17.172.224.47'
self
.
us_region_data
=
{
'region_
nam
e'
:
'CA'
,
'region_
cod
e'
:
'CA'
,
'country_code'
:
'US'
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment