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
bd359ab1
Commit
bd359ab1
authored
Feb 22, 2013
by
William Tisäter
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into sudan-fix
parents
b6462152
534fa941
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
42 deletions
+13
-42
MANIFEST.in
+8
-0
pygeoip/timezone.py
+0
-0
pygeoip/util.py
+5
-42
No files found.
MANIFEST.in
0 → 100644
View file @
bd359ab1
recursive-include apidocs *
recursive-include tests *
include README.md
include COPYING
include DEVELOPER
include INSTALL
include epydoc.ini
include tox.ini
pygeoip/timezone.py
View file @
bd359ab1
This diff is collapsed.
Click to expand it.
pygeoip/util.py
View file @
bd359ab1
...
...
@@ -20,11 +20,8 @@ You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
"""
import
struct
import
socket
from
array
import
array
from
pygeoip.const
import
PY3
import
binascii
def
ip2long
(
ip
):
...
...
@@ -33,41 +30,7 @@ def ip2long(ip):
@param ip: IPv4 or IPv6 address
@type ip: str
"""
if
ip
.
find
(
':'
)
>=
0
:
return
ip2long_v6
(
ip
)
else
:
return
ip2long_v4
(
ip
)
def
ip2long_v4
(
ip
):
"""
Convert a IPv4 address into a 32-bit integer.
@param ip: quad-dotted IPv4 address
@type ip: str
@return: network byte order 32-bit integer
@rtype: int
"""
ip_array
=
ip
.
split
(
'.'
)
if
PY3
:
# int and long are unified in py3
return
int
(
ip_array
[
0
])
*
16777216
+
int
(
ip_array
[
1
])
*
65536
+
\
int
(
ip_array
[
2
])
*
256
+
int
(
ip_array
[
3
])
else
:
return
long
(
ip_array
[
0
])
*
16777216
+
long
(
ip_array
[
1
])
*
65536
+
\
long
(
ip_array
[
2
])
*
256
+
long
(
ip_array
[
3
])
def
ip2long_v6
(
ip
):
"""
Convert a IPv6 address into long.
@param ip: IPv6 address
@type ip: str
@return: network byte order long
@rtype: long
"""
ipbyte
=
socket
.
inet_pton
(
socket
.
AF_INET6
,
ip
)
ipnum
=
array
(
'L'
,
struct
.
unpack
(
'!4L'
,
ipbyte
))
max_index
=
len
(
ipnum
)
-
1
return
sum
(
ipnum
[
max_index
-
i
]
<<
(
i
*
32
)
for
i
in
range
(
len
(
ipnum
)))
try
:
return
int
(
binascii
.
hexlify
(
socket
.
inet_aton
(
ip
)),
16
)
except
socket
.
error
:
return
int
(
binascii
.
hexlify
(
socket
.
inet_pton
(
socket
.
AF_INET6
,
ip
)),
16
)
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