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

Archive Download this file

Branches

Tags