Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown using IPV6 address #19

Closed
cosmoJFH opened this issue Mar 19, 2022 · 3 comments
Closed

Exception thrown using IPV6 address #19

cosmoJFH opened this issue Mar 19, 2022 · 3 comments

Comments

@cosmoJFH
Copy link

cosmoJFH commented Mar 19, 2022

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).

@cosmoJFH
Copy link
Author

We fixed the issue making use of the following change

def is_ipv4(hostname):
    pattern = r'^([0-9]{1,3}[.]){3}[0-9]{1,3}$'
    if match(pattern, hostname) is not None:
        ip_parts = hostname.split('.')
        for i in range(0,len(ip_parts)):
            if int(ip_parts[i]) > 255:
                return False
        return 4
    return False

ip2location added a commit that referenced this issue Mar 21, 2022
Fixed GitHub issue #19.
@ip2location
Copy link
Collaborator

Hi cosmoJFH,

Thanks for the fix! We had made a fix on this and had made a new release.

@KirylKovaliov
Copy link

KirylKovaliov commented May 11, 2022

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants