Skip to content

Commit

Permalink
Rework current_ip()
Browse files Browse the repository at this point in the history
Use the same logic used in reprounzip-docker: connect to google.com,
then reads the local address on the socket.
  • Loading branch information
remram44 committed Sep 7, 2017
1 parent e1a6d4c commit de09cae
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions vistrails/core/system/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,43 @@ def current_user():
def current_ip():
""" current_ip() -> str
Gets current IP address trying to avoid the IPv6 interface """
# Try to resolve the local hostname
try:
info = socket.getaddrinfo(socket.gethostname(), None)
addresses = socket.getaddrinfo(socket.gethostname(), None)
except socket.gaierror:
pass
else:
# Try to find an IPv4
for i in info:
for i in addresses:
if i[0] == socket.AF_INET:
return i[4][0]
# Return any address
for i in info:
for i in addresses:
if i[0] in (socket.AF_INET, socket.AF_INET6):
return i[4][0]
except Exception, e:
debug.unexpected_exception(e)
return ''

# That didn't work, use whatever address lets you connect to google.com
try:
addresses = socket.getaddrinfo('google.com', 9, socket.AF_UNSPEC,
socket.SOCK_STREAM)
except socket.gaierror:
pass
else:
for address in addresses:
sock = None
try:
af, socktype, proto, canonname, sa = address
sock = socket.socket(af, socktype, proto)
sock.settimeout(1)
sock.connect(sa)
sock.close()
except socket.error:
pass
if sock is not None:
return sock.getsockname()[0]

# Couldn't find anything
return ''

def current_time():
"""current_time() -> datetime.datetime
Expand Down

0 comments on commit de09cae

Please sign in to comment.