【发布时间】:2015-01-11 00:42:26
【问题描述】:
import socket
import struct
def get_default_gateway_linux():
with open("/proc/net/route") as fh:
for line in fh:
fields = line.strip().split()
if fields[1] != '00000000' or not int(fields[3], 16) & 2:
continue
return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16)))
def getNetworkIp():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('bt', 0))
return s.getsockname()[0]
print "Your Machine IP has been detected as "+getNetworkIp()
print "Your Gateway IP has been detected as "+get_default_gateway_linux()
上面的代码在回溯 5 R3 中执行时既没有显示任何错误也没有任何输出 请帮助我处理此代码!
【问题讨论】:
标签: python ip output local gateway