Skip to content
This repository was archived by the owner on Oct 29, 2019. It is now read-only.

Commit fe226a7

Browse files
committed
third time's a charm for get_ip?
1 parent 24f5c7b commit fe226a7

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

network/utils.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
44
:author: Connor Henley @thatging3rkid
55
"""
6+
import fcntl
67
import socket
8+
import struct
79

8-
# shamelessly stolen from https://stackoverflow.com/a/1267524
9-
def get_ip():
10-
try:
11-
return [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1][0]
12-
except:
13-
return "127.0.0.1"
10+
# shamelessly stolen from https://stackoverflow.com/a/24196955
11+
def get_ip(ifname):
12+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
13+
return socket.inet_ntoa(fcntl.ioctl(
14+
s.fileno(),
15+
0x8915, # SIOCGIFADDR
16+
struct.pack('256s', ifname[:15])
17+
)[20:24])

0 commit comments

Comments
 (0)