Skip to content

Commit

Permalink
init.
Browse files Browse the repository at this point in the history
  • Loading branch information
lxyu committed Mar 5, 2014
0 parents commit a7b19bf
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.pyc
MANIFEST
dist
Binary file added IP/17monipdb.dat
Binary file not shown.
3 changes: 3 additions & 0 deletions IP/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__all__ = ["find"]

from .ip import find
68 changes: 68 additions & 0 deletions IP/ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import sys

PY2 = sys.version_info.major == 2

import functools
import os
import socket
import struct

_unpack_V = lambda b: struct.unpack("<L", b)
_unpack_N = lambda b: struct.unpack(">L", b)
_unpack_C = lambda b: struct.unpack("B", b)

with open(os.path.join(os.path.dirname(__file__), "17monipdb.dat"), "rb") as f:
dat = f.read()
offset, = _unpack_N(dat[:4])
index = dat[4:offset]


def memoize(func):
"""Memoize for functions based on memory
"""
cache = func.cache = {}

@functools.wraps(func)
def wrapper(*args, **kwargs):
key = "{0}{1}".format(args, kwargs)
if key not in cache:
cache[key] = func(*args, **kwargs)
return cache[key]
return wrapper


@memoize
def _find_ip(ip):
nip = socket.inet_aton(ip)

tmp_offset = int(ip.split(".")[0]) * 4
start, = _unpack_V(index[tmp_offset:tmp_offset + 4])

index_offset = index_length = 0
max_comp_len = offset - 1028
start = start * 8 + 1024
while start < max_comp_len:
if index[start:start + 4] >= nip:
index_offset, = _unpack_V(
index[start + 4:start + 7] + chr(0).encode("utf-8"))
if PY2:
index_length, = _unpack_C(index[start + 7])
else:
index_length = index[start + 7]
break
start += 8

if index_offset == 0:
return

res_offset = offset + index_offset - 1024
return dat[res_offset:res_offset + index_length].decode("utf-8")


def find(ip):
try:
ip = socket.gethostbyname(ip)
except socket.gaierror:
return

return _find_ip(ip)
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.rst
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
17MonIP Python Lib
==================

IP search based on 17mon.cn, the best IP database for china.

Source: http://tool.17mon.cn

Install
-------

.. code:: bash
$ pip install 17monip
Usage
-----

.. code:: python
>>> import IP
>>> IP.find("www.baidu.com")
'中国\t浙江'
>>> IP.find("127.0.0.1")
'本机地址\t本机地址'
18 changes: 18 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from distutils.core import setup

setup(
name="17MonIP",
version="0.1.0",
description="IP search based on 17mon.cn, the best IP database for China.",
author="Lx Yu",
author_email="[email protected]",
packages=["IP", ],
package_data={'IP': ['17monipdb.dat'], },
entry_points={"console_scripts": ["ip = ip.cmd:ip", ]},
url="http://lxyu.github.io/17monip/",
license="MIT",
long_description=open("README.rst").read(),
)

0 comments on commit a7b19bf

Please sign in to comment.