##// END OF EJS Templates
utils.localinterfaces: Add PUBLIC_IPS to consolidate socket logic...
W. Trevor King -
Show More
@@ -1,44 +1,51
1 1 """Simple utility for building a list of local IPs using the socket module.
2 2 This module defines two constants:
3 3
4 4 LOCALHOST : The loopback interface, or the first interface that points to this
5 5 machine. It will *almost* always be '127.0.0.1'
6 6
7 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 13 # Copyright (C) 2010-2011 The IPython Development Team
11 14 #
12 15 # Distributed under the terms of the BSD License. The full license is in
13 16 # the file COPYING, distributed as part of this software.
14 17 #-----------------------------------------------------------------------------
15 18
16 19 #-----------------------------------------------------------------------------
17 20 # Imports
18 21 #-----------------------------------------------------------------------------
19 22
20 23 import socket
21 24
22 25 from .data import uniq_stable
23 26
24 27 #-----------------------------------------------------------------------------
25 28 # Code
26 29 #-----------------------------------------------------------------------------
27 30
28 31 LOCAL_IPS = []
29 32 try:
30 33 LOCAL_IPS = socket.gethostbyname_ex('localhost')[2]
31 34 except socket.gaierror:
32 35 pass
33 36
37 PUBLIC_IPS = []
34 38 try:
35 LOCAL_IPS.extend(socket.gethostbyname_ex(socket.gethostname())[2])
39 PUBLIC_IPS = socket.gethostbyname_ex(socket.gethostname())[2]
36 40 except socket.gaierror:
37 41 pass
42 else:
43 PUBLIC_IPS = uniq_stable(PUBLIC_IPS)
44 LOCAL_IPS.extend(PUBLIC_IPS)
38 45
39 46 # include all-interface aliases: 0.0.0.0 and ''
40 47 LOCAL_IPS.extend(['0.0.0.0', ''])
41 48
42 49 LOCAL_IPS = uniq_stable(LOCAL_IPS)
43 50
44 51 LOCALHOST = LOCAL_IPS[0]
General Comments 0
You need to be logged in to leave comments. Login now