blob: 13c4d02bba9eb6739fb83f71d38d6ca2a3a43dfc [file] [log] [blame]
Tor Norbye1aa2e092014-08-20 17:01:23 -07001
2from _pydev_imps import _pydev_socket as socket
Tor Norbye3a2425a2013-11-04 10:16:08 -08003
4_cache = None
5def get_localhost():
6 '''
7 Should return 127.0.0.1 in ipv4 and ::1 in ipv6
Tor Norbye1aa2e092014-08-20 17:01:23 -07008
Tor Norbye3a2425a2013-11-04 10:16:08 -08009 localhost is not used because on windows vista/windows 7, there can be issues where the resolving doesn't work
Tor Norbye1aa2e092014-08-20 17:01:23 -070010 properly and takes a lot of time (had this issue on the pyunit server).
11
Tor Norbye3a2425a2013-11-04 10:16:08 -080012 Using the IP directly solves the problem.
13 '''
14 #TODO: Needs better investigation!
Tor Norbye1aa2e092014-08-20 17:01:23 -070015
Tor Norbye3a2425a2013-11-04 10:16:08 -080016 global _cache
17 if _cache is None:
18 try:
19 for addr_info in socket.getaddrinfo("localhost", 80, 0, 0, socket.SOL_TCP):
20 config = addr_info[4]
21 if config[0] == '127.0.0.1':
22 _cache = '127.0.0.1'
23 return _cache
24 except:
25 #Ok, some versions of Python don't have getaddrinfo or SOL_TCP... Just consider it 127.0.0.1 in this case.
26 _cache = '127.0.0.1'
27 else:
28 _cache = 'localhost'
29
30 return _cache
31
32
33def get_socket_name():
34 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
35 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
36 sock.bind(('', 0))
37 return sock.getsockname()