##// END OF EJS Templates
utils.localinterfaces: Add PUBLIC_IPS to consolidate socket logic...
W. Trevor King -
Show More
@@ -1,44 +1,51 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
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.
8 """
11 """
9 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
10 # Copyright (C) 2010-2011 The IPython Development Team
13 # Copyright (C) 2010-2011 The IPython Development Team
11 #
14 #
12 # 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
13 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
15
18
16 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
17 # Imports
20 # Imports
18 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
19
22
20 import socket
23 import socket
21
24
22 from .data import uniq_stable
25 from .data import uniq_stable
23
26
24 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
25 # Code
28 # Code
26 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
27
30
28 LOCAL_IPS = []
31 LOCAL_IPS = []
29 try:
32 try:
30 LOCAL_IPS = socket.gethostbyname_ex('localhost')[2]
33 LOCAL_IPS = socket.gethostbyname_ex('localhost')[2]
31 except socket.gaierror:
34 except socket.gaierror:
32 pass
35 pass
33
36
37 PUBLIC_IPS = []
34 try:
38 try:
35 LOCAL_IPS.extend(socket.gethostbyname_ex(socket.gethostname())[2])
39 PUBLIC_IPS = socket.gethostbyname_ex(socket.gethostname())[2]
36 except socket.gaierror:
40 except socket.gaierror:
37 pass
41 pass
42 else:
43 PUBLIC_IPS = uniq_stable(PUBLIC_IPS)
44 LOCAL_IPS.extend(PUBLIC_IPS)
38
45
39 # include all-interface aliases: 0.0.0.0 and ''
46 # include all-interface aliases: 0.0.0.0 and ''
40 LOCAL_IPS.extend(['0.0.0.0', ''])
47 LOCAL_IPS.extend(['0.0.0.0', ''])
41
48
42 LOCAL_IPS = uniq_stable(LOCAL_IPS)
49 LOCAL_IPS = uniq_stable(LOCAL_IPS)
43
50
44 LOCALHOST = LOCAL_IPS[0]
51 LOCALHOST = LOCAL_IPS[0]
General Comments 0
You need to be logged in to leave comments. Login now