Show More
@@ -12,7 +12,7 b' PUBLIC_IPS : A list of public IP addresses that point to this machine.' | |||
|
12 | 12 | Use these to tell remote clients where to find you. |
|
13 | 13 | """ |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 |
# Copyright (C) 2010 |
|
|
15 | # Copyright (C) 2010 The IPython Development Team | |
|
16 | 16 | # |
|
17 | 17 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | 18 | # the file COPYING, distributed as part of this software. |
@@ -22,9 +22,13 b' PUBLIC_IPS : A list of public IP addresses that point to this machine.' | |||
|
22 | 22 | # Imports |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | import os | |
|
25 | 26 | import socket |
|
26 | 27 | |
|
27 | 28 | from .data import uniq_stable |
|
29 | from .process import get_output_error_code | |
|
30 | from .py3compat import bytes_to_str | |
|
31 | from .warn import warn | |
|
28 | 32 | |
|
29 | 33 | #----------------------------------------------------------------------------- |
|
30 | 34 | # Code |
@@ -53,6 +57,84 b' def _requires_ips(f):' | |||
|
53 | 57 | return f(*args, **kwargs) |
|
54 | 58 | return ips_loaded |
|
55 | 59 | |
|
60 | # subprocess-parsing ip finders | |
|
61 | class NoIPAddresses(Exception): | |
|
62 | pass | |
|
63 | ||
|
64 | def _populate_from_list(addrs): | |
|
65 | """populate local and public IPs from flat list of all IPs""" | |
|
66 | if not addrs: | |
|
67 | raise NoIPAddresses | |
|
68 | ||
|
69 | global LOCALHOST | |
|
70 | public_ips = [] | |
|
71 | local_ips = [] | |
|
72 | ||
|
73 | for ip in addrs: | |
|
74 | local_ips.append(ip) | |
|
75 | if not ip.startswith('127.'): | |
|
76 | public_ips.append(ip) | |
|
77 | elif not LOCALHOST: | |
|
78 | LOCALHOST = ip | |
|
79 | ||
|
80 | if not LOCALHOST: | |
|
81 | LOCALHOST = '127.0.0.1' | |
|
82 | local_ips.insert(0, LOCALHOST) | |
|
83 | ||
|
84 | local_ips.extend(['0.0.0.0', '']) | |
|
85 | ||
|
86 | LOCAL_IPS[:] = uniq_stable(local_ips) | |
|
87 | PUBLIC_IPS[:] = uniq_stable(public_ips) | |
|
88 | ||
|
89 | def _load_ips_ifconfig(): | |
|
90 | """load ip addresses from `ifconfig` output (posix)""" | |
|
91 | ||
|
92 | out, err, rc = get_output_error_code('ifconfig') | |
|
93 | if rc: | |
|
94 | # no ifconfig, it's usually in /sbin and /sbin is not on everyone's PATH | |
|
95 | out, err, rc = get_output_error_code('/sbin/ifconfig') | |
|
96 | if rc: | |
|
97 | raise IOError("no ifconfig: %s" % err) | |
|
98 | ||
|
99 | lines = bytes_to_str(out).splitlines() | |
|
100 | addrs = [] | |
|
101 | for line in lines: | |
|
102 | blocks = line.lower().split() | |
|
103 | if blocks[0] == 'inet': | |
|
104 | addrs.append(blocks[1]) | |
|
105 | _populate_from_list(addrs) | |
|
106 | ||
|
107 | ||
|
108 | def _load_ips_ip(): | |
|
109 | """load ip addresses from `ip addr` output (Linux)""" | |
|
110 | out, err, rc = get_output_error_code('ip addr') | |
|
111 | if rc: | |
|
112 | raise IOError("no ip: %s" % err) | |
|
113 | ||
|
114 | lines = bytes_to_str(out).splitlines() | |
|
115 | addrs = [] | |
|
116 | for line in lines: | |
|
117 | blocks = line.lower().split() | |
|
118 | if blocks[0] == 'inet': | |
|
119 | addrs.append(blocks[1].split('/')[0]) | |
|
120 | _populate_from_list(addrs) | |
|
121 | ||
|
122 | ||
|
123 | def _load_ips_ipconfig(): | |
|
124 | """load ip addresses from `ipconfig` output (Windows)""" | |
|
125 | out, err, rc = get_output_error_code('ipconfig') | |
|
126 | if rc: | |
|
127 | raise IOError("no ipconfig: %s" % err) | |
|
128 | ||
|
129 | lines = bytes_to_str(out).splitlines() | |
|
130 | addrs = ['127.0.0.1'] | |
|
131 | for line in lines: | |
|
132 | line = line.lower().split() | |
|
133 | if line[:2] == ['ipv4', 'address']: | |
|
134 | addrs.append(line.split()[-1]) | |
|
135 | _populate_from_list(addrs) | |
|
136 | ||
|
137 | ||
|
56 | 138 | def _load_ips_netifaces(): |
|
57 | 139 | """load ip addresses with netifaces""" |
|
58 | 140 | import netifaces |
@@ -81,6 +163,7 b' def _load_ips_netifaces():' | |||
|
81 | 163 | LOCAL_IPS[:] = uniq_stable(local_ips) |
|
82 | 164 | PUBLIC_IPS[:] = uniq_stable(public_ips) |
|
83 | 165 | |
|
166 | ||
|
84 | 167 | def _load_ips_gethostbyname(): |
|
85 | 168 | """load ip addresses with socket.gethostbyname_ex |
|
86 | 169 | |
@@ -104,7 +187,7 b' def _load_ips_gethostbyname():' | |||
|
104 | 187 | finally: |
|
105 | 188 | PUBLIC_IPS[:] = uniq_stable(PUBLIC_IPS) |
|
106 | 189 | LOCAL_IPS.extend(PUBLIC_IPS) |
|
107 | ||
|
190 | ||
|
108 | 191 | # include all-interface aliases: 0.0.0.0 and '' |
|
109 | 192 | LOCAL_IPS.extend(['0.0.0.0', '']) |
|
110 | 193 | |
@@ -125,17 +208,42 b' def _load_ips():' | |||
|
125 | 208 | |
|
126 | 209 | This function will only ever be called once. |
|
127 | 210 | |
|
128 |
It will use netifaces to do it quickly if available |
|
|
129 | otherwise it will fallback on socket.gethostbyname_ex, which can be slow. | |
|
211 | It will use netifaces to do it quickly if available. | |
|
212 | Then it will fallback on parsing the output of ifconfig / ip addr / ipconfig, as appropriate. | |
|
213 | Finally, it will fallback on socket.gethostbyname_ex, which can be slow. | |
|
130 | 214 | """ |
|
215 | ||
|
131 | 216 | try: |
|
217 | # first priority, use netifaces | |
|
132 | 218 | try: |
|
133 | _load_ips_netifaces() | |
|
219 | return _load_ips_netifaces() | |
|
134 | 220 | except ImportError: |
|
135 | _load_ips_gethostbyname() | |
|
136 | except Exception: | |
|
221 | pass | |
|
222 | ||
|
223 | # second priority, parse subprocess output (how reliable is this?) | |
|
224 | ||
|
225 | if os.name == 'nt': | |
|
226 | try: | |
|
227 | return _load_ips_ipconfig() | |
|
228 | except (IOError, NoIPAddresses): | |
|
229 | pass | |
|
230 | else: | |
|
231 | try: | |
|
232 | return _load_ips_ifconfig() | |
|
233 | except (IOError, NoIPAddresses): | |
|
234 | pass | |
|
235 | try: | |
|
236 | return _load_ips_ip() | |
|
237 | except (IOError, NoIPAddresses): | |
|
238 | pass | |
|
239 | ||
|
240 | # lowest priority, use gethostbyname | |
|
241 | ||
|
242 | return _load_ips_gethostbyname() | |
|
243 | except Exception as e: | |
|
137 | 244 | # unexpected error shouldn't crash, load dumb default values instead. |
|
138 | _load_ips_dumb() | |
|
245 | warn("Unexpected error discovering local network interfaces: %s" % e) | |
|
246 | _load_ips_dumb() | |
|
139 | 247 | |
|
140 | 248 | |
|
141 | 249 | @_requires_ips |
General Comments 0
You need to be logged in to leave comments.
Login now