Should return 127.0.0.1 in ipv4 and ::1 in ipv6 localhost is not used because on windows vista/windows 7, there can be issues where the resolving doesn't work properly and takes a lot of time (had this issue on the pyunit server). Using the IP directly solves the problem.
()
| 7 | |
| 8 | |
| 9 | def get_localhost(): |
| 10 | """ |
| 11 | Should return 127.0.0.1 in ipv4 and ::1 in ipv6 |
| 12 | |
| 13 | localhost is not used because on windows vista/windows 7, there can be issues where the resolving doesn't work |
| 14 | properly and takes a lot of time (had this issue on the pyunit server). |
| 15 | |
| 16 | Using the IP directly solves the problem. |
| 17 | """ |
| 18 | # TODO: Needs better investigation! |
| 19 | |
| 20 | global _cache |
| 21 | if _cache is None: |
| 22 | try: |
| 23 | for addr_info in socket.getaddrinfo("localhost", 80, 0, 0, socket.SOL_TCP): |
| 24 | config = addr_info[4] |
| 25 | if config[0] == "127.0.0.1": |
| 26 | _cache = "127.0.0.1" |
| 27 | return _cache |
| 28 | except: |
| 29 | # Ok, some versions of Python don't have getaddrinfo or SOL_TCP... Just consider it 127.0.0.1 in this case. |
| 30 | _cache = "127.0.0.1" |
| 31 | else: |
| 32 | _cache = "localhost" |
| 33 | |
| 34 | return _cache |
| 35 | |
| 36 | |
| 37 | def get_socket_names(n_sockets, close=False): |
no outgoing calls