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
|
2016-02-17 18:15:04 +01:00
|
|
|
import sys
|
|
|
|
import socket
|
2016-02-04 20:39:50 +01:00
|
|
|
from iptogeo import IPToGeo, IPToGeoException
|
2016-01-31 11:42:28 +01:00
|
|
|
|
2016-02-04 20:39:50 +01:00
|
|
|
TIMEOUT = None
|
|
|
|
# TIMEOUT = 5.0
|
|
|
|
|
2016-02-17 18:15:04 +01:00
|
|
|
iptogeo = IPToGeo(timeout=TIMEOUT, family=socket.AF_INET6, remote_addr='::1')
|
2016-02-04 20:39:50 +01:00
|
|
|
|
2016-02-04 20:39:50 +01:00
|
|
|
def get_random_ip_v4():
|
|
|
|
ip = '%d.%d.%d.%d' % \
|
|
|
|
(ord(os.urandom(1)), ord(os.urandom(1)), ord(os.urandom(1)), ord(os.urandom(1)))
|
|
|
|
return ip
|
2016-01-31 11:42:28 +01:00
|
|
|
|
2016-02-04 20:39:50 +01:00
|
|
|
def test_ip(ip, verbose=True, proxy=iptogeo):
|
2016-02-04 20:39:50 +01:00
|
|
|
try:
|
2016-02-04 20:39:50 +01:00
|
|
|
(ipres, country_code) = proxy.ip_to_geo(ip)
|
2016-02-04 20:39:50 +01:00
|
|
|
except IPToGeoException, e:
|
2016-02-04 20:39:50 +01:00
|
|
|
if verbose:
|
|
|
|
print 'Error with IP %s' % (ip)
|
|
|
|
print e
|
2016-02-04 20:39:50 +01:00
|
|
|
return
|
2016-01-31 11:42:28 +01:00
|
|
|
|
2016-02-04 20:39:50 +01:00
|
|
|
if verbose:
|
|
|
|
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)
|
2016-01-31 11:42:28 +01:00
|
|
|
|
2016-02-04 20:39:50 +01:00
|
|
|
print '#### Coherence test'
|
2016-01-31 11:42:28 +01:00
|
|
|
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
|
|
|
print '#### 5 sockets test'
|
|
|
|
for i in range(0, 6):
|
|
|
|
print 'Test %d' % (i)
|
|
|
|
test_ip(get_random_ip_v4(), verbose=False, proxy=IPToGeo(timeout=TIMEOUT))
|
2016-01-31 11:42:28 +01:00
|
|
|
|
2016-02-04 20:39:50 +01:00
|
|
|
print '#### 35 sockets test'
|
|
|
|
for i in range(0, 36):
|
|
|
|
print 'Test %d' % (i)
|
|
|
|
test_ip(get_random_ip_v4(), verbose=False, proxy=IPToGeo(timeout=TIMEOUT))
|
|
|
|
|
|
|
|
print '#### 1 socket heavy test'
|
2016-02-04 20:39:50 +01:00
|
|
|
for i in range(0, 1000):
|
2016-02-04 20:39:50 +01:00
|
|
|
print 'Test %d' % (i)
|
|
|
|
test_ip(get_random_ip_v4(), verbose=False)
|
|
|
|
|
|
|
|
print '#### 35 sockets heavy test'
|
|
|
|
geo_proxy = []
|
|
|
|
for i in range(0, 36):
|
|
|
|
geo_proxy.append(IPToGeo(timeout=TIMEOUT))
|
|
|
|
|
|
|
|
for i in range(0, 1000):
|
|
|
|
print 'Test %d' % (i)
|
|
|
|
for proxy in geo_proxy:
|
|
|
|
test_ip(get_random_ip_v4(), verbose=False, proxy=proxy)
|
2016-02-04 20:39:50 +01:00
|
|
|
|
|
|
|
print 'All is OK for me'
|