Get local usable IP and port Returns ------- str IP address, e.g., '192.168.8.12:50051'
()
| 38 | |
| 39 | |
| 40 | def get_local_usable_addr(): |
| 41 | """Get local usable IP and port |
| 42 | |
| 43 | Returns |
| 44 | ------- |
| 45 | str |
| 46 | IP address, e.g., '192.168.8.12:50051' |
| 47 | """ |
| 48 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 49 | try: |
| 50 | # doesn't even have to be reachable |
| 51 | sock.connect(("10.255.255.255", 1)) |
| 52 | ip_addr = sock.getsockname()[0] |
| 53 | except ValueError: |
| 54 | ip_addr = "127.0.0.1" |
| 55 | finally: |
| 56 | sock.close() |
| 57 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 58 | sock.bind(("", 0)) |
| 59 | sock.listen(1) |
| 60 | port = sock.getsockname()[1] |
| 61 | sock.close() |
| 62 | |
| 63 | return ip_addr + " " + str(port) |
| 64 | |
| 65 | |
| 66 | def prepare_dist(): |