##// END OF EJS Templates
Merge pull request #4189 from minrk/localinterfaces-finally...
Paul Ivanov -
r12478:3ce4b25d merge
parent child Browse files
Show More
@@ -1,55 +1,55 b''
1 """Simple utility for building a list of local IPs using the socket module.
1 """Simple utility for building a list of local IPs using the socket module.
2 This module defines two constants:
2 This module defines two constants:
3
3
4 LOCALHOST : The loopback interface, or the first interface that points to this
4 LOCALHOST : The loopback interface, or the first interface that points to this
5 machine. It will *almost* always be '127.0.0.1'
5 machine. It will *almost* always be '127.0.0.1'
6
6
7 LOCAL_IPS : A list of IP addresses, loopback first, that point to this machine.
7 LOCAL_IPS : A list of IP addresses, loopback first, that point to this machine.
8
8
9 PUBLIC_IPS : A list of public IP addresses that point to this machine.
9 PUBLIC_IPS : A list of public IP addresses that point to this machine.
10 Use these to tell remote clients where to find you.
10 Use these to tell remote clients where to find you.
11 """
11 """
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2010-2011 The IPython Development Team
13 # Copyright (C) 2010-2011 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import socket
23 import socket
24
24
25 from .data import uniq_stable
25 from .data import uniq_stable
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Code
28 # Code
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 LOCAL_IPS = []
31 LOCAL_IPS = []
32 try:
32 try:
33 LOCAL_IPS = socket.gethostbyname_ex('localhost')[2]
33 LOCAL_IPS = socket.gethostbyname_ex('localhost')[2]
34 except socket.error:
34 except socket.error:
35 pass
35 pass
36
36
37 PUBLIC_IPS = []
37 PUBLIC_IPS = []
38 try:
38 try:
39 hostname = socket.gethostname()
39 hostname = socket.gethostname()
40 PUBLIC_IPS = socket.gethostbyname_ex(hostname)[2]
40 PUBLIC_IPS = socket.gethostbyname_ex(hostname)[2]
41 # try hostname.local, in case hostname has been short-circuited to loopback
41 # try hostname.local, in case hostname has been short-circuited to loopback
42 if not hostname.endswith('.local') and all(ip.startswith('127') for ip in PUBLIC_IPS):
42 if not hostname.endswith('.local') and all(ip.startswith('127') for ip in PUBLIC_IPS):
43 PUBLIC_IPS = socket.gethostbyname_ex(socket.gethostname() + '.local')[2]
43 PUBLIC_IPS = socket.gethostbyname_ex(socket.gethostname() + '.local')[2]
44 except socket.error:
44 except socket.error:
45 pass
45 pass
46 else:
46 finally:
47 PUBLIC_IPS = uniq_stable(PUBLIC_IPS)
47 PUBLIC_IPS = uniq_stable(PUBLIC_IPS)
48 LOCAL_IPS.extend(PUBLIC_IPS)
48 LOCAL_IPS.extend(PUBLIC_IPS)
49
49
50 # include all-interface aliases: 0.0.0.0 and ''
50 # include all-interface aliases: 0.0.0.0 and ''
51 LOCAL_IPS.extend(['0.0.0.0', ''])
51 LOCAL_IPS.extend(['0.0.0.0', ''])
52
52
53 LOCAL_IPS = uniq_stable(LOCAL_IPS)
53 LOCAL_IPS = uniq_stable(LOCAL_IPS)
54
54
55 LOCALHOST = LOCAL_IPS[0]
55 LOCALHOST = LOCAL_IPS[0]
General Comments 0
You need to be logged in to leave comments. Login now