IP to Geo

IP to Geo Git Source Tree

Root/tests/test_ip_to_geo.py

1#!/usr/bin/env python
2# -*- coding: utf-8
3
4import os
5from iptogeo import IPToGeo, IPToGeoException
6
7TIMEOUT = None
8# TIMEOUT = 5.0
9
10iptogeo = IPToGeo(timeout=TIMEOUT)
11
12def get_random_ip_v4():
13 ip = '%d.%d.%d.%d' % \
14 (ord(os.urandom(1)), ord(os.urandom(1)), ord(os.urandom(1)), ord(os.urandom(1)))
15 return ip
16
17def test_ip(ip, verbose=True, proxy=iptogeo):
18 try:
19 (ipres, country_code) = proxy.ip_to_geo(ip)
20 except IPToGeoException, e:
21 if verbose:
22 print 'Error with IP %s' % (ip)
23 print e
24 return
25
26 if verbose:
27 if not country_code:
28 print 'Country code for %s (%08x) not found' % (ip, ipres)
29 else:
30 print 'Country code for %s (%08x) is %s' % (ip, ipres, country_code)
31
32print '#### Coherence test'
33test_ip('1.5.7.3')
34test_ip('1.5.255.4')
35test_ip('1.6.255.4')
36test_ip('2.0.0.0')
37test_ip('127.0.0.1')
38test_ip('1.55.3.12')
39test_ip('1.57.0.0')
40
41print '#### 5 sockets test'
42for i in range(0, 6):
43 print 'Test %d' % (i)
44 test_ip(get_random_ip_v4(), verbose=False, proxy=IPToGeo(timeout=TIMEOUT))
45
46print '#### 35 sockets test'
47for i in range(0, 36):
48 print 'Test %d' % (i)
49 test_ip(get_random_ip_v4(), verbose=False, proxy=IPToGeo(timeout=TIMEOUT))
50
51print '#### 1 socket heavy test'
52for i in range(0, 1000):
53 print 'Test %d' % (i)
54 test_ip(get_random_ip_v4(), verbose=False)
55
56print '#### 35 sockets heavy test'
57geo_proxy = []
58for i in range(0, 36):
59 geo_proxy.append(IPToGeo(timeout=TIMEOUT))
60
61for i in range(0, 1000):
62 print 'Test %d' % (i)
63 for proxy in geo_proxy:
64 test_ip(get_random_ip_v4(), verbose=False, proxy=proxy)
65
66print 'All is OK for me'

Archive Download this file

Branches

Tags