Update for Python3

This commit is contained in:
2020-11-10 01:47:47 +00:00
parent fbfb32f8b1
commit 85aaf1385e
7 changed files with 936 additions and 722 deletions

View File

@@ -58,7 +58,7 @@ class Interface:
while (self.running):
tty_in = os.read(self.m, readLen)
for tty_out in self.handleInput(tty_in):
os.write(self.m, tty_out)
os.write(self.m, tty_out.encode())
##%% handleInput(tty_in)
@@ -67,7 +67,7 @@ class Interface:
## address. That device must handle the command via dev.handleCMD(cmd, args)
##
def handleInput(self, tty_in):
tty_in = tty_in.strip()
tty_in = tty_in.strip().decode()
if tty_in != "" and self.addr in self.devices:
dev = self.devices[self.addr]
@@ -83,5 +83,5 @@ class Interface:
##
def addDevice(self, dev, addr):
self.devices[addr] = dev

View File

@@ -0,0 +1,36 @@
# Use those functions to enumerate all interfaces available on the system using Python.
# found on <http://code.activestate.com/recipes/439093/#c1>
import socket
import fcntl
import struct
import array
def all_interfaces():
max_possible = 128 # arbitrary. raise if needed.
bytes = max_possible * 32
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
names = array.array('B', '\0' * bytes)
outbytes = struct.unpack('iL', fcntl.ioctl(
s.fileno(),
0x8912, # SIOCGIFCONF
struct.pack('iL', bytes, names.buffer_info()[0])
))[0]
namestr = names.tostring()
lst = []
for i in range(0, outbytes, 40):
name = namestr[i:i+16].split('\0', 1)[0]
ip = namestr[i+20:i+24]
lst.append((name, ip))
return lst
def format_ip(addr):
return str(ord(addr[0])) + '.' + \
str(ord(addr[1])) + '.' + \
str(ord(addr[2])) + '.' + \
str(ord(addr[3]))
ifs = all_interfaces()
for i in ifs:
print "%12s %s" % (i[0], format_ip(i[1]))