iptogeo/tests/test_ip_to_geo.py

34 lines
747 B
Python
Raw Normal View History

2016-01-31 11:42:28 +01:00
#!/usr/bin/env python
# -*- coding: utf-8
2016-02-04 20:39:50 +01:00
import os
from iptogeo import IPToGeo, IPToGeoException
2016-01-31 11:42:28 +01:00
iptogeo = IPToGeo()
def test_ip(ip):
2016-02-04 20:39:50 +01:00
try:
(ipres, country_code) = iptogeo.ip_to_geo(ip)
except IPToGeoException, e:
print e
return
2016-01-31 11:42:28 +01:00
if not country_code:
print 'Country code for %s (%08x) not found' % (ip, ipres)
else:
print 'Country code for %s (%08x) is %s' % (ip, ipres, country_code)
test_ip('1.5.7.3')
test_ip('1.5.255.4')
test_ip('1.6.255.4')
test_ip('2.0.0.0')
test_ip('127.0.0.1')
test_ip('1.55.3.12')
test_ip('1.57.0.0')
2016-02-04 20:39:50 +01:00
for i in range(0, 1000):
ip = '%d.%d.%d.%d' % \
(ord(os.urandom(1)), ord(os.urandom(1)), ord(os.urandom(1)), ord(os.urandom(1)))
test_ip(ip)