##// 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 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 8
9 9 PUBLIC_IPS : A list of public IP addresses that point to this machine.
10 10 Use these to tell remote clients where to find you.
11 11 """
12 12 #-----------------------------------------------------------------------------
13 13 # Copyright (C) 2010-2011 The IPython Development Team
14 14 #
15 15 # Distributed under the terms of the BSD License. The full license is in
16 16 # the file COPYING, distributed as part of this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 import socket
24 24
25 25 from .data import uniq_stable
26 26
27 27 #-----------------------------------------------------------------------------
28 28 # Code
29 29 #-----------------------------------------------------------------------------
30 30
31 31 LOCAL_IPS = []
32 32 try:
33 33 LOCAL_IPS = socket.gethostbyname_ex('localhost')[2]
34 34 except socket.error:
35 35 pass
36 36
37 37 PUBLIC_IPS = []
38 38 try:
39 39 hostname = socket.gethostname()
40 40 PUBLIC_IPS = socket.gethostbyname_ex(hostname)[2]
41 41 # try hostname.local, in case hostname has been short-circuited to loopback
42 42 if not hostname.endswith('.local') and all(ip.startswith('127') for ip in PUBLIC_IPS):
43 43 PUBLIC_IPS = socket.gethostbyname_ex(socket.gethostname() + '.local')[2]
44 44 except socket.error:
45 45 pass
46 else:
46 finally:
47 47 PUBLIC_IPS = uniq_stable(PUBLIC_IPS)
48 48 LOCAL_IPS.extend(PUBLIC_IPS)
49 49
50 50 # include all-interface aliases: 0.0.0.0 and ''
51 51 LOCAL_IPS.extend(['0.0.0.0', ''])
52 52
53 53 LOCAL_IPS = uniq_stable(LOCAL_IPS)
54 54
55 55 LOCALHOST = LOCAL_IPS[0]
General Comments 0
You need to be logged in to leave comments. Login now