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

Archive Download this file

Branches

Tags