Ajout de la fonction permettant de récupérer l'adresse IP.

This commit is contained in:
Cacahuete (RPi) 2019-11-02 17:19:28 +01:00
parent 6c38e6cbcb
commit d7d0671f38

18
get_routable_ip.py Normal file
View file

@ -0,0 +1,18 @@
"""Some useful functions."""
import socket
def get_ip():
"""Return the primary, routable, IP address.
From https://stackoverflow.com/a/28950776
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP