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
05322189
Commit
05322189
authored
Sep 28, 2012
by
William Tisäter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix PEP8 violations and convert dos format to unix
parent
3c3225b9
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
29 additions
and
22 deletions
+29
-22
INSTALL
+0
-0
README.md
+0
-0
pygeoip/__init__.py
+24
-18
pygeoip/const.py
+0
-0
pygeoip/util.py
+5
-3
setup.py
+0
-1
No files found.
INSTALL
View file @
05322189
README.md
View file @
05322189
pygeoip/__init__.py
View file @
05322189
# -*- coding: utf-8 -*-
"""
Pure Python GeoIP API. The API is based off of U{MaxMind's C-based Python API<http://www.maxmind.com/app/python>},
but the code itself is based on the U{pure PHP5 API<http://pear.php.net/package/Net_GeoIP/>}
by Jim Winstead and Hans Lellelid.
Pure Python GeoIP API
It is mostly a drop-in replacement, except the
C{new} and C{open} methods are gone. You should instantiate the L{GeoIP} class yourself:
The API is based on U{MaxMind's C-based Python
API<http://www.maxmind.com/app/python>}, but the code itself is based on
the U{pure PHP5 API<http://pear.php.net/package/Net_GeoIP/>} by Jim Winstead
and Hans Lellelid.
It is mostly a drop-in replacement, except the C{new} and C{open} methods
are gone. You should instantiate the L{GeoIP} class yourself:
C{gi = GeoIP('/path/to/GeoIP.dat', pygeoip.MEMORY_CACHE)}
...
...
@@ -63,9 +66,9 @@ class GeoIPMetaclass(type):
"""
Singleton method to gets an instance without reparsing the db. Unique
instances are instantiated based on the filename of the db. Flags are
ignored for this, i.e. if you initialize one with STANDARD
flag (default)
and then try later to initialize with MEMORY_CACHE, it will still
return the STANDARD one.
ignored for this, i.e. if you initialize one with STANDARD
flag (default) and then try later to initialize with MEMORY_CACHE, it
will still
return the STANDARD one.
"""
if
not
hasattr
(
cls
,
'_instances'
):
...
...
@@ -150,9 +153,9 @@ class GeoIP(GeoIPBase):
for
i
in
range
(
const
.
STRUCTURE_INFO_MAX_SIZE
):
chars
=
chr
(
255
)
*
3
encodin
g
=
'unicode_escape'
fla
g
=
'unicode_escape'
delim
=
self
.
_filehandle
.
read
(
3
)
if
(
delim
==
chars
)
if
PY3
else
(
delim
==
unicode
(
chars
,
encodin
g
)):
if
(
delim
==
chars
)
if
PY3
else
(
delim
==
unicode
(
chars
,
fla
g
)):
self
.
_databaseType
=
ord
(
self
.
_filehandle
.
read
(
1
))
# Backwards compatibility with databases from
...
...
@@ -289,7 +292,7 @@ class GeoIP(GeoIPBase):
country_code
=
rec
[
'country_code'
]
if
'country_code'
in
rec
else
''
region
=
rec
[
'region_name'
]
if
'region_name'
in
rec
else
''
return
{
'country_code'
:
country_code
,
'region_name'
:
region
}
return
{
'country_code'
:
country_code
,
'region_name'
:
region
}
def
_get_record
(
self
,
ipnum
):
"""
...
...
@@ -363,7 +366,7 @@ class GeoIP(GeoIPBase):
if
record
[
'country_code'
]
==
'US'
:
for
j
in
range
(
3
):
char
=
ord
(
record_buf
[
record_buf_pos
])
dmaarea_combo
+=
(
char
<<
(
j
*
8
))
dmaarea_combo
+=
(
char
<<
(
j
*
8
))
record_buf_pos
+=
1
record
[
'dma_code'
]
=
int
(
math
.
floor
(
dmaarea_combo
/
1000
))
...
...
@@ -422,13 +425,16 @@ class GeoIP(GeoIPBase):
@rtype: str
"""
try
:
COUNTRY
_EDITIONS
=
(
const
.
COUNTRY_EDITION
,
const
.
COUNTRY_EDITION_V6
)
if
self
.
_databaseType
in
COUNTRY
_EDITIONS
:
VALID
_EDITIONS
=
(
const
.
COUNTRY_EDITION
,
const
.
COUNTRY_EDITION_V6
)
if
self
.
_databaseType
in
VALID
_EDITIONS
:
ipv
=
6
if
addr
.
find
(
':'
)
>=
0
else
4
if
ipv
==
4
and
self
.
_databaseType
!=
const
.
COUNTRY_EDITION
:
raise
ValueError
(
'Invalid database type; expected IPv6 address'
)
message
=
'Invalid database type; expected IPv6 address'
raise
ValueError
(
message
)
if
ipv
==
6
and
self
.
_databaseType
!=
const
.
COUNTRY_EDITION_V6
:
raise
ValueError
(
'Invalid database type; expected IPv4 address'
)
message
=
'Invalid database type; expected IPv4 address'
raise
ValueError
(
message
)
country_id
=
self
.
id_by_addr
(
addr
)
...
...
@@ -465,8 +471,8 @@ class GeoIP(GeoIPBase):
@rtype: str
"""
try
:
COUNTRY
_EDITIONS
=
(
const
.
COUNTRY_EDITION
,
const
.
COUNTRY_EDITION_V6
)
if
self
.
_databaseType
in
COUNTRY
_EDITIONS
:
VALID
_EDITIONS
=
(
const
.
COUNTRY_EDITION
,
const
.
COUNTRY_EDITION_V6
)
if
self
.
_databaseType
in
VALID
_EDITIONS
:
return
const
.
COUNTRY_NAMES
[
self
.
id_by_addr
(
addr
)]
elif
self
.
_databaseType
in
const
.
CITY_EDITIONS
:
return
self
.
record_by_addr
(
addr
)[
'country_name'
]
...
...
pygeoip/const.py
View file @
05322189
This diff is collapsed.
Click to expand it.
pygeoip/util.py
View file @
05322189
# -*- coding: utf-8 -*-
"""
Misc. utility functions. It is part of the pygeoip package.
...
...
@@ -51,10 +52,11 @@ def ip2long_v4(ip):
ip_array
=
ip
.
split
(
'.'
)
if
PY3
:
# int and long are unified in py3
ip_long
=
int
(
ip_array
[
0
])
*
16777216
+
int
(
ip_array
[
1
])
*
65536
+
int
(
ip_array
[
2
])
*
256
+
int
(
ip_array
[
3
])
return
int
(
ip_array
[
0
])
*
16777216
+
int
(
ip_array
[
1
])
*
65536
+
\
int
(
ip_array
[
2
])
*
256
+
int
(
ip_array
[
3
])
else
:
ip_long
=
long
(
ip_array
[
0
])
*
16777216
+
long
(
ip_array
[
1
])
*
65536
+
long
(
ip_array
[
2
])
*
256
+
long
(
ip_array
[
3
])
return
ip_long
return
long
(
ip_array
[
0
])
*
16777216
+
long
(
ip_array
[
1
])
*
65536
+
\
long
(
ip_array
[
2
])
*
256
+
long
(
ip_array
[
3
])
def
ip2long_v6
(
ip
):
...
...
setup.py
View file @
05322189
#!/usr/bin/env python
"""
Setup file for pygeoip package.
...
...
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