Move from recursive algorithm to linear one Update REQ_IPV4 and REQ_IPV6 defines

This commit is contained in:
2016-02-04 20:39:50 +01:00
parent 36d5b0f43b
commit 57fce2afe2
6 changed files with 67 additions and 57 deletions

View File

@@ -13,8 +13,8 @@ class IPToGeo(object):
VERSION = 1
REQ = 1
RESP = 0
IPV4 = 32
IPV6 = 128
IPV4 = 4
IPV6 = 16
IP_NOT_FOUND = 6
@@ -40,13 +40,13 @@ class IPToGeo(object):
self._socket.settimeout(self._timeout)
self._socket.connect((self._remote_addr, self._remote_port))
def _create_request(self, int_ip):
def _create_request(self, ip):
packet = ''
packet += struct.pack('<IBBBBI', IPToGeo.MAGIC, IPToGeo.VERSION, IPToGeo.REQ,
0, #err
IPToGeo.IPV4, # ip type
0) # flags
packet += struct.pack('<I', int_ip) # ipv4
packet += struct.pack('<BBBB', ip[0], ip[1], ip[2], ip[3]) # ipv4
packet += struct.pack('<III', 0, 0, 0) # ipv6
packet += struct.pack('<I', 0) # country code
@@ -68,12 +68,8 @@ class IPToGeo(object):
def ip_to_geo(self, ip):
splitted_ip = [int(a) for a in ip.split('.')]
int_ip = splitted_ip[0] << 24
int_ip |= splitted_ip[1] << 16
int_ip |= splitted_ip[2] << 8
int_ip |= splitted_ip[3] << 0
packet = self._create_request(int_ip)
packet = self._create_request(splitted_ip)
try:
self._socket.send(packet)
except IOError, e: