-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver_address.py
More file actions
executable file
·57 lines (45 loc) · 1.62 KB
/
server_address.py
File metadata and controls
executable file
·57 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python3
# -----------------------------------------------------------
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2019-2020 Datadog, Inc.
# -----------------------------------------------------------
import socket
class ServerAddress():
def __init__(self, ip, port):
self.ip = ip
self.port = port
def get_private_IP_on_subnet(subnet_broadcast: str):
"""
Returns private IP on a specific local network or `None` if the local network is not reachable.
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect((subnet_broadcast, 1))
return ServerAddress(s.getsockname()[0], 8000)
except:
return None
finally:
s.close()
def get_private_IP():
"""
Returns private IP on the local network or `None` if the local network is not reachable.
"""
private_ip = get_private_IP_on_subnet('10.255.255.255')
if private_ip is not None:
return private_ip
return get_private_IP_on_subnet('192.168.255.255')
def get_localhost():
"""
Returns localhost address.
"""
return ServerAddress('127.0.0.1', 8000)
def get_best_server_address():
"""
Returns private IP if possible, localhost otherwise.
"""
private_ip = get_private_IP()
return private_ip if private_ip is not None else get_localhost()
if __name__ == "__main__":
address = get_best_server_address()
print("{ip}:{port}".format( ip = address.ip, port = address.port))