Giter Site home page Giter Site logo

chrislim2888 / ip2location-python Goto Github PK

View Code? Open in Web Editor NEW
138.0 12.0 48.0 6.71 MB

This module is a Python Library that enables the user to find the country, region, city, coordinates, zip code, ISP, domain name, timezone, connection speed, IDD code, area code, weather station code, weather station name, mobile, usage types, address type and IAB category that any IP address or host name originates from.

Home Page: https://www.ip2location.com

License: MIT License

Python 100.00%
ip2location python ip2location-python python-library ip-location iplocation geolocation ip-address ipv4 ipv6

ip2location-python's People

Contributors

chrislim2888 avatar ip2location avatar vstoykov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ip2location-python's Issues

A way to support ipv4 and ipv6 at same time

IP2Location is currently distributing the ipv4 and ipv6 databases in different files. This library does not support opening both at the same time. So we need to load correct file based on the requested ip address, but there is no method to check which version needs to be loaded, so for now we used the private method _parse_addr. Our code looks something like this:

iplocation = IP2Location()
if iplocation._parse_addr(addr)[0] == 6:
  iplocation.open('ipv6.bin')
else:
  iplocation.open('ipv4.bin')
return iplocation.get_all(addr)

If there is better way to support ipv4 and 6 at same time, can you please show how to do it? Or could you add public method that tells which ip database file is needed? For example:

  def get_ip_version(addr):
    return this._parse_addr(addr)[0]

Unable to query IPv6 addresses

Hello,

We have problems using ip2location database with IPv6 addresses. For IPv4 addresses it is working fine. Should we be converting IPv6 to some other format or what would be the issue here?

Database we are using is named: IP-COUNTRY-REGION-CITY-ISP-DOMAIN.BIN

In [1]: import IP2Location

In [2]: iplocation = IP2Location.IP2Location()

In [3]: iplocation.open(settings.IP2LOCATION_PATH)

In [4]: iplocation.get_all('66.249.93.239') # Works fine
Out[4]: {'country_short': u'US', 'city': u'Mountain View', 'domain': u'google.com', 'ip': '66.249.88.0', 'region': u'California', 'isp': u'Google Inc.', 'country_long': u'United States'}

In [5]: iplocation.get_all('2a02:c200:1:10:2:4:7382:1') # Throws error
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-5-ddd13222b424> in <module>()
----> 1 iplocation.get_all('2a02:c200:1:10:2:4:7382:1')

.../lib/python2.7/site-packages/IP2Location.pyc in get_all(self, addr)
    198             Returns IP2LocationRecord or None if address not found in file
    199         '''
--> 200         return self._get_record(addr)
    201 
    202     def find(self, addr):

.../lib/python2.7/site-packages/IP2Location.pyc in _get_record(self, ip)
    357         while low <= high:
    358             mid = int((low + high) / 2)
--> 359             ipfrom = self._readip(baseaddr + (mid) * (self._dbcolumn * 4 + off), ipv)
    360             ipto = self._readip(baseaddr + (mid + 1) * (self._dbcolumn * 4 + off), ipv)
    361 

.../lib/python2.7/site-packages/IP2Location.pyc in _readip(self, offset, ipv)
    228             return self._readi(offset)
    229         elif ipv == 6:
--> 230             a, b, c, d = self._readi(offset), self._readi(offset + 4), self._readi(offset + 8), self._readi(offset + 12)
    231             return (d << 96) | (c << 64) | (b << 32) | a
    232 

.../lib/python2.7/site-packages/IP2Location.pyc in _readi(self, offset)
    217 
    218     def _readi(self, offset):
--> 219         self._f.seek(offset - 1)
    220         return struct.unpack('<I', self._f.read(4))[0]
    221 

IOError: [Errno 22] Invalid argument

In [6]: 

Getting Exception on IPv6

Getting same error on 8.7.3 which seems to be the latest. 8.7.2 works fine for me

from IP2Location import IP2Location
IP2Location(filename="db11.bin").get_all('2806:2f0:9180:8eec:f026:f24c:8a97:e6a2')
Traceback (most recent call last):
  File "/home/disposer/work/regula/v2-licensing-portal/src/tests/local/ip2location_test.py", line 3, in <module>
    IP2Location(filename="db11.bin").get_all('2806:2f0:9180:8eec:f026:f24c:8a97:e6a2')
  File "/home/disposer/.cache/pypoetry/virtualenvs/app-dJLMS_bJ-py3.8/lib/python3.8/site-packages/IP2Location.py", line 315, in get_all
    return self._get_record(addr)
  File "/home/disposer/.cache/pypoetry/virtualenvs/app-dJLMS_bJ-py3.8/lib/python3.8/site-packages/IP2Location.py", line 546, in _get_record
    ipv, ipnum = self._parse_addr(ip)
  File "/home/disposer/.cache/pypoetry/virtualenvs/app-dJLMS_bJ-py3.8/lib/python3.8/site-packages/IP2Location.py", line 477, in _parse_addr
    if is_ipv4(addr) == 4 and '256' not in addr:
  File "/home/disposer/.cache/pypoetry/virtualenvs/app-dJLMS_bJ-py3.8/lib/python3.8/site-packages/IP2Location.py", line 101, in is_ipv4
    if int(ip_parts[i]) > 255:
ValueError: invalid literal for int() with base 10: '2806:2f0:9180:8eec:f026:f24c:8a97:e6a2'

Originally posted by @KirylKovaliov in #19 (comment)

File IP2Location.py not found for manual installation of 8.10.1

Since the build I get via pip is not working at all, I tried to install 8.10.1 manually from the release you provided.

Steps to reproduce

  1. wget -c https://github.com/chrislim2888/IP2Location-Python/archive/refs/tags/8.10.1.zip
  2. unzip 8.10.1.zip
  3. cd IP2Location-Python-8.10.1
  4. python3 setup.py build

Expected outcome

The build should run without any error so that I can continue with the last step: python3 setup.py install

Actual outcome

[user@server IP2Location-Python-8.10.1]$ python3 setup.py build
running build
running build_py
file IP2Location.py (for module IP2Location) not found
creating build
creating build/lib
creating build/lib/IP2Location
copying IP2Location/webservice.py -> build/lib/IP2Location
copying IP2Location/region.py -> build/lib/IP2Location
copying IP2Location/iptools.py -> build/lib/IP2Location
copying IP2Location/database.py -> build/lib/IP2Location
copying IP2Location/country.py -> build/lib/IP2Location
copying IP2Location/__init__.py -> build/lib/IP2Location
file IP2Location.py (for module IP2Location) not found

System information

  • Python Version: Python 3.6.8 and Python 3.11.5
  • OS: Rocky Linux 8.9 (Green Obsidian)
  • Kernel: Linux 4.18.0 x86_64 GNU/Linux

IP2Location binary

I'm wondering if IPv6 binary file also contains all the entries of the IPv4 binary file? I'm looking to do IPv4 & IPv6 lookups but the CSV format version only show IPv4Comp for all IPv4 fields in the IPv6 CSV file. I'd like to have them combined so that I only need to handle binary file. Is it possible, or do you supply these files by any chance?

This is not official IP2Location right?

I see that on PyPi latest version is 7.0.3 from 19.10.2015. Last commit here is from 2014. I wanted to contribute some little improvements but this repo is not up to date.

Did you know where I can propose some changes?

OSError: illegal IP address string passed to inet_pton

Excuse me~ I've a weird situation
In k8s, it raise this exception

>>> import IP2Location
>>> IP2LOCOBJ = IP2Location.IP2Location()
>>> IP2LOCOBJ.open("IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE.BIN")
>>> IP2LOCOBJ.get_all('2002:1a1:8fbf:0:79d0:7fec:e2be:2bcc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/airflow/src/8-0-0/IP2Location.py", line 219, in get_all
    return self._get_record(addr)
  File "/usr/local/airflow/src/8-0-0/IP2Location.py", line 385, in _get_record
    ipno = struct.unpack('!L', socket.inet_pton(socket.AF_INET, ip))[0]
OSError: illegal IP address string passed to inet_pton
>>> IP2LOCOBJ.get_all('2002:1a1:8fbf:0:79d0:7fec:e2be:2bcc')

But when i run the same code in my mac

It was just fine

>>> IP2LOCOBJ.get_all('2002:24e6:e916:0:18c0:7265:aa7b:386c')
{'ip': '60258737847196048878978323620', 'country_short': b'-', 'country_long': b'-', 'region': b'-', 'city': b'-', 'latitude': 0.0, 'longitude': 0.0}

Do you have any idea?

IP2Location giving wrong geolocation with updated database in Python 3.6.

I have a module in my application where I am showing the geolocation of a user using their IP Address. The sample code for that module is shown below:

image

I am using IP2Location API (version 8.6.4) on Python 3.6. I am using the paid database (.BIN file) for the lookup, and have recently downloaded the latest database file. As mentioned in the image above, I am inputting the IP Address 76.100.80.65, which is the IP of a user in Frederick, Maryland. However, when I run the code, the output is:

image

These coordinates point to an address in Alexandria, Virginia as shown in Google Maps:

image

Kindly help me with this problem. Thank you!

"struct.error: unpack requires a string argument of length 4" with BIN database file retrieved on September 22, 2017

Hello,

I am encountering an error of type "struct.error: unpack requires a string argument of length 4" while using the IP2Location-8.0.0 module with the IP2LOCATION-LITE-DB11.BIN fiel I retrieved today (September 22nd). The stack trace states the following:

File "batch_locate.py", line 302, in locate_ip_i2l
    response = IP2Loc_v4.get_all(ip_addr)
  File "/Users/afressancourt/.virtualenvs/py-multi-geoloc/lib/python2.7/site-packages/IP2Location.py", line 219, in get_all
    return self._get_record(addr)
  File "/Users/afressancourt/.virtualenvs/py-multi-geoloc/lib/python2.7/site-packages/IP2Location.py", line 388, in _get_record
    ipfrom = self._readip(baseaddr + (mid) * (self._dbcolumn * 4 + off), ipv)
  File "/Users/afressancourt/.virtualenvs/py-multi-geoloc/lib/python2.7/site-packages/IP2Location.py", line 248, in _readip
    return self._readi(offset)
  File "/Users/afressancourt/.virtualenvs/py-multi-geoloc/lib/python2.7/site-packages/IP2Location.py", line 240, in _readi
    return struct.unpack('<I', self._f.read(4))[0]
struct.error: unpack requires a string argument of length 4

Python 3.7 Compatibility

Hello,

I seem to be having problems using the module in Python 3.7. Pip is reporting that it's installed correctly but when I attempt to import I'm told the module isn't available.

>>> import IP2Location
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'IP2Location'

I had a look in site packages and I'm seeing different files between a 8.0 installation against 3.6 and a 8.0.2 install against 3.7:

./lib/python3.6/site-packages/IP2Location.py
./lib/python3.6/site-packages/__pycache__/IP2Location.cpython-36.pyc
./lib/python3.6/site-packages/IP2Location-8.0.0.dist-info
./lib/python3.6/site-packages/IP2Location-8.0.0.dist-info/RECORD
./lib/python3.6/site-packages/IP2Location-8.0.0.dist-info/WHEEL
./lib/python3.6/site-packages/IP2Location-8.0.0.dist-info/top_level.txt
./lib/python3.6/site-packages/IP2Location-8.0.0.dist-info/INSTALLER
./lib/python3.6/site-packages/IP2Location-8.0.0.dist-info/METADATA
./lib/python3.7/site-packages/IP2Location-8.0.2.dist-info
./lib/python3.7/site-packages/IP2Location-8.0.2.dist-info/RECORD
./lib/python3.7/site-packages/IP2Location-8.0.2.dist-info/WHEEL
./lib/python3.7/site-packages/IP2Location-8.0.2.dist-info/top_level.txt
./lib/python3.7/site-packages/IP2Location-8.0.2.dist-info/INSTALLER
./lib/python3.7/site-packages/IP2Location-8.0.2.dist-info/METADATA

Here's the Pip output as well from an install for what it's worth:

Collecting IP2Location
  Downloading https://files.pythonhosted.org/packages/c1/c0/3d4bea4261e332ddc9c85e69c51f32bb0b58c84c9d7e6ce29a2272a32f71/IP2Location-8.0.2-py3-none-any.whl
Installing collected packages: IP2Location
Successfully installed IP2Location-8.0.2

Exception thrown using IPV6 address

When I tried to get the location for an IPV6,

database = IP2Location.IP2Location()
database.open(os.path.join("data", "IP2LOCATION-LITE-DB1.IPV6.BIN"))
rec = database.get_all('2600:1009:d01f:87d4:0:48:63ee:4601')

the following exception is thrown:

Traceback (most recent call last):
  File "prueba.py", line 77, in <module>
    print(str(IP2LocObj.get_all('2600:1009:d01f:87d4:0:48:63ee:4601')))
  File "anaconda3/lib/python3.7/site-packages/IP2Location-8.7.1-py3.7.egg/IP2Location.py", line 315, in get_all
  File "anaconda3/lib/python3.7/site-packages/IP2Location-8.7.1-py3.7.egg/IP2Location.py", line 546, in _get_record
  File "anaconda3/lib/python3.7/site-packages/IP2Location-8.7.1-py3.7.egg/IP2Location.py", line 477, in _parse_addr
  File "anaconda3/lib/python3.7/site-packages/IP2Location-8.7.1-py3.7.egg/IP2Location.py", line 101, in is_ipv4
ValueError: invalid literal for int() with base 10: '2600:1009:d01f:87d4:0:48:63ee:4601'

The problem is that the method _parse_addr(self, addr) always calls the method is_ipv4(addr).

IP2Location returning Junk Data for ISP/City/Country

Hi,

I am using ip2location bin file to retrieve details of an IP. Many a times i get some junk data for City, isp, lat/long.
Has someone faced this issue ??
We are not calling these API's in parallel. All are sequential calls. Any help is highly appreciated.

Updates vs shared memory

What would be the correct behaviour to handle updates when database is used in shared memory mode?

There is a long running set of gunicorn processes that uses ip2location database in shared memory mode and some side job that updates database file periodically. Does mmap hanles updates itself or its developer responsibility to re-read database?

Add type annotations

I would like to contribute this feature. It would require dropping Python2 support.

Possible bug?

✗ pip install IP2Location
Requirement already satisfied: IP2Location in ./venv/lib/python3.8/site-packages (8.6.0)
(venv) ➜ ✗ python
Python 3.8.2 (default, Dec 21 2020, 15:06:04)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import IP2Location
>>> IP2LocObj = IP2Location.IP2Location()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../venv/lib/python3.8/site-packages/IP2Location.py", line 155, in __init__
    if os.path.isfile(filename) == False:
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/genericpath.py", line 30, in isfile
    st = os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

Debug "print" statements left in code

With the latest release it looks like there are a couple debug "print" statements that were left in, the are on line 569 and 584 in IP2Location.py. Can they be cleaned up or at least changed to be optional?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.