IP to Geo

IP to Geo Git Source Tree

Root/tests/iptogeo.py

1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright 2016 Grégory Soutadé
6#
7# This file is part of iptogeo.
8#
9# iptogeo is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# iptogeo is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with iptogeo. If not, see <http://www.gnu.org/licenses/>.
21#
22
23import socket
24import struct
25
26class IPToGeoException(Exception):
27 pass
28
29class IPToGeo(object):
30
31 MAGIC = 0x179E08EF
32 VERSION = 1
33 REQ = 1
34 RESP = 0
35 IPV4 = 4
36 IPV6 = 16
37
38 IP_NOT_FOUND = 6
39
40 PACKET_SIZE = 32
41
42 ERRORS = {1 : 'Bad magic',
43 2 : 'Bad version',
44 3 : 'Bad request field' ,
45 4 : 'Bad IP version',
46 5 : 'Unsupported IP version',
47 6 : 'IP not found'}
48
49 def __init__(self, remote_addr='127.0.0.1', remote_port=53333, timeout=None, family=socket.AF_INET):
50 self._remote_addr = remote_addr
51 self._remote_port = remote_port
52 self._timeout = timeout
53 self._family = family
54
55 self._create_socket()
56
57 def _create_socket(self):
58 self._socket = socket.socket(self._family, socket.SOCK_STREAM)
59 if not self._timeout is None:
60 self._socket.settimeout(self._timeout)
61 self._socket.connect((self._remote_addr, self._remote_port))
62
63 def _create_request(self, ip):
64 packet = ''
65 packet += struct.pack('<IBBBBI', IPToGeo.MAGIC, IPToGeo.VERSION, IPToGeo.REQ,
66 0, #err
67 IPToGeo.IPV4, # ip type
68 0) # flags
69 packet += struct.pack('<BBBB', ip[0], ip[1], ip[2], ip[3]) # ipv4
70 packet += struct.pack('<III', 0, 0, 0) # ipv6
71 packet += struct.pack('<I', 0) # country code
72
73 return packet
74
75 def _check_request(self, packet):
76 (magic, version, req, err, ip_type, flags, ipv4, ipv6b, ipv6c, ipv6d) = struct.unpack_from('<IBBBBIIIII', packet, 0)
77
78 if magic != IPToGeo.MAGIC:
79 raise IPToGeoException('Invalid magic %08x' % (magic))
80
81 if err == IPToGeo.IP_NOT_FOUND: return (ipv4, None) # IP not found
82 if err != 0:
83 raise IPToGeoException(IPToGeo.ERRORS[err])
84
85 (cc0, cc1, cc2, cc3) = struct.unpack_from('BBBB', packet, 7*4)
86
87 return (ipv4, '%c%c%c%c' % (cc0, cc1, cc2, cc3))
88
89 def ip_to_geo(self, ip):
90 splitted_ip = [int(a) for a in ip.split('.')]
91
92 packet = self._create_request(splitted_ip)
93 try:
94 self._socket.send(packet)
95 except IOError, e:
96 # Give another chance (we may have been disconnected due to timeout)
97 self._create_socket()
98 self._socket.send(packet)
99 packet = self._socket.recv(IPToGeo.PACKET_SIZE)
100 if not packet:
101 raise IPToGeoException('Error, empty packet')
102 (ip, country_code) = self._check_request(packet)
103 if country_code:
104 # convert to string
105 country_code = '%c%c' % (country_code[0], country_code[1])
106 return (ip, country_code)
107
108 def close(self):
109 self._socket.close()

Archive Download this file

Branches

Tags