IP to Geo

IP to Geo Commit Details

Date:2020-05-11 10:07:10 (5 months 18 days ago)
Author:Grégory Soutadé
Branch:master
Commit:ea4bfda214b80cd86bdb0d446f7bdc739f0aec1e
Parents: 13ef026003ab9ec9e97ff8f8ac46a969ab24c6f3
Message:Handle timeouts & max requests in Python test class

Changes:
Mtests/iptogeo.py (3 diffs)

File differences

tests/iptogeo.py
4545
4646
4747
48
48
49
50
4951
5052
5153
5254
5355
54
55
56
57
5658
5759
60
61
5862
5963
6064
......
108112
109113
110114
111
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
112134
113135
114136
......
124146
125147
126148
127
128
129
130
131
132
133
134
135
149
136150
137151
138152
4 : 'Bad IP version',
5 : 'Unsupported IP version',
6 : 'IP not found'}
MAX_REQUESTS = 50
def __init__(self, remote_addr='127.0.0.1', remote_port=53333, timeout=None, family=socket.AF_INET):
self._remote_addr = remote_addr
self._remote_port = remote_port
self._timeout = timeout
self._family = family
self._create_socket()
self._nb_requests_sent = self.MAX_REQUESTS # Force socket creation
self._socket = None
def _create_socket(self):
if self._socket:
self._socket.close()
self._socket = socket.socket(self._family, socket.SOCK_STREAM)
if not self._timeout is None:
self._socket.settimeout(self._timeout)
(cc0, cc1, cc2, cc3) = struct.unpack_from('BBBB', packet, 7*4)
return (ip_res, '%c%c%c%c' % (cc0, cc1, cc2, cc3))
def _send_request(self, packet, second_chance=True):
self._nb_requests_sent += 1
if self._nb_requests_sent >= self.MAX_REQUESTS:
self._create_socket()
self._nb_requests_sent = 0
try:
self._socket.send(packet)
packet = self._socket.recv(IPToGeo.PACKET_SIZE)
if not packet:
raise socket.timeout
return packet
except socket.timeout, e:
if second_chance:
self._nb_requests_sent = self.MAX_REQUESTS
return self._send_request(packet, False)
else:
raise e
def ip_to_geo(self, ip):
ip_type = IPToGeo.IPV4
if ip.find('.') >= 0:
raise Exception('Bad IP %s' % (ip))
packet = self._create_request(splitted_ip, ip_type)
try:
self._socket.send(packet)
except IOError, e:
# Give another chance (we may have been disconnected due to timeout)
self._create_socket()
self._socket.send(packet)
packet = self._socket.recv(IPToGeo.PACKET_SIZE)
if not packet:
raise IPToGeoException('Error, empty packet')
packet = self._send_request(packet)
(ip, country_code) = self._check_request(packet)
if country_code:
# convert to string

Archive Download the corresponding diff file

Branches

Tags