Skip to content
Snippets Groups Projects
Verified Commit 59bde2bf authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

get primary ip address | refs #28817

parent 96c18469
No related branches found
No related tags found
No related merge requests found
......@@ -179,7 +179,7 @@ def check_spf(ip_addr: str, sender: str, domain: str) -> tuple:
errors = 0
if ip_address(ip_addr).is_private:
u.info("{} is a private address, cannot check SPF")
u.info("{} is a private address, cannot check SPF".format(ip_addr))
elif ip_addr and sender:
# check spf
result, _ = spf.check2(i=ip_addr, s=domain, h="")
......@@ -215,6 +215,7 @@ def main():
(socket.gethostbyname(relay) if relay else None)
or conf.get("NETWORK_IP_NAT")
or conf.get("NETWORK_IP")
or u.get_ip()
or None
)
sender = conf.get("EMAIL_SENDER") or None
......
......@@ -449,3 +449,23 @@ def mkcert(
cert_dir + "/cert.pem",
]
)
def get_ip() -> str:
"""Get the "primary" ip address, the one used by the default route.
:return: IP address
:rtype: str
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't have to be reachable
s.connect(("10.255.255.255", 1))
IP = s.getsockname()[0]
except Exception:
IP = "127.0.0.1"
finally:
s.close()
return IP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment