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