Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.

Commit 3f091d4

Browse files
Etienne-Carriereyurishkuro
authored andcommitted
Enable windows support by isolating ioctl calls (#233)
* Enable windows support by isolating ioctl calls Signed-off-by: Etienne Carriere <[email protected]> * Remove the case when its returns anything but an IP Signed-off-by: Etienne Carriere <[email protected]> * Add basic tests for get_local_ip_by_socket Signed-off-by: Etienne Carriere <[email protected]>
1 parent 38524ad commit 3f091d4

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

jaeger_client/utils.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from six.moves import range
16-
17-
import fcntl
1815
import socket
1916
import struct
17+
2018
import time
19+
from six.moves import range
2120

2221

2322
class ErrorReporter(object):
@@ -65,27 +64,54 @@ def local_ip():
6564
except IOError:
6665
ip = socket.gethostbyname('localhost')
6766
if ip.startswith('127.'):
68-
# Check eth0, eth1, eth2, en0, ...
69-
interfaces = [
70-
i + bytes(n) for i in (b'eth', b'en', b'wlan') for n in range(3)
71-
] # :(
72-
for interface in interfaces:
73-
try:
74-
ip = interface_ip(interface)
67+
ip = get_local_ip_by_interfaces()
68+
if ip is None:
69+
ip = get_local_ip_by_socket()
70+
return ip
71+
72+
73+
def get_local_ip_by_socket():
74+
# Explanation : https://stackoverflow.com/questions/166506
75+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
76+
try:
77+
# doesn't even have to be reachable
78+
s.connect(('10.255.255.255', 1))
79+
ip = s.getsockname()[0]
80+
except IOError:
81+
ip = None
82+
finally:
83+
s.close()
84+
return ip
85+
86+
87+
def get_local_ip_by_interfaces():
88+
ip = None
89+
# Check eth0, eth1, eth2, en0, ...
90+
interfaces = [
91+
i + bytes(n) for i in (b'eth', b'en', b'wlan') for n in range(3)
92+
] # :(
93+
for interface in interfaces:
94+
try:
95+
ip = interface_ip(interface)
96+
if ip is not None:
7597
break
76-
except IOError:
77-
pass
98+
except IOError:
99+
pass
78100
return ip
79101

80102

81103
def interface_ip(interface):
82-
"""Determine the IP assigned to us by the given network interface."""
83-
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
84-
return socket.inet_ntoa(
85-
fcntl.ioctl(
86-
sock.fileno(), 0x8915, struct.pack('256s', interface[:15])
87-
)[20:24]
88-
)
104+
try:
105+
import fcntl
106+
"""Determine the IP assigned to us by the given network interface."""
107+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
108+
return socket.inet_ntoa(
109+
fcntl.ioctl(
110+
sock.fileno(), 0x8915, struct.pack('256s', interface[:15])
111+
)[20:24]
112+
)
113+
except ImportError:
114+
return None
89115
# Explanation:
90116
# http://stackoverflow.com/questions/11735821/python-get-localhost-ip
91117
# http://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python

tests/test_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ def test_local_ip_does_not_blow_up():
7676
with mock.patch('socket.gethostbyname',
7777
side_effect=[IOError(), '127.0.0.1']):
7878
jaeger_client.utils.local_ip()
79+
80+
def test_get_local_ip_by_socket_does_not_blow_up():
81+
import jaeger_client.utils
82+
jaeger_client.utils.get_local_ip_by_socket()

0 commit comments

Comments
 (0)