Hello and thanks. :-)
No, unfortunately what you ask is not possible. Sockets have no notion of routing in that sense.
What you could do is get a list of all source addresses as in:
>>> local_addrs = [x.laddr[0] for x in psutil.net_connections()]
>>> local_addrs
['0.0.0.0', '0.0.0.0', '192.168.1.6', '192.168.33.1', '::', '127.0.0.1', '192.168.1.6', '192.168.1.6', '192.168.1.6', '::', '192.168.33.1', '0.0.0.0', '192.168.1.6', '::', '192.168.1.6', '0.0.0.0', '0.0.0.0', '192.168.1.6', '192.168.1.6', '0.0.0.0', '::', '192.168.1.6', '192.168.1.6', '::', '0.0.0.0', '192.168.0.131', '192.168.1.6', '192.168.1.6', '192.168.1.6', '0.0.0.0', '192.168.1.6', '0.0.0.0', '192.168.0.131', '0.0.0.0', '192.168.0.131', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.33.1', '192.168.1.6', '0.0.0.0', '::', '192.168.1.6', '0.0.0.0', '::', '192.168.0.131', '0.0.0.0', '192.168.1.6', '192.168.1.6', '::', '::', '127.0.0.1', '192.168.1.6', '0.0.0.0', '127.0.0.1', '192.168.1.6', '192.168.1.6', '0.0.0.0', '0.0.0.0', '192.168.1.6', '::', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.0.131', '192.168.1.6', '192.168.1.6', '192.168.1.6', '127.0.1.1', '127.0.1.1', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.1.6', '::', '192.168.1.6', '::', '192.168.50.1', '192.168.1.6', '0.0.0.0', '::', '192.168.1.6', '192.168.1.6', '192.168.1.6', '0.0.0.0', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.1.6', '0.0.0.0', '::', '0.0.0.0', '192.168.1.6', '192.168.1.6', '::', '0.0.0.0', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.50.1', '192.168.1.6', '192.168.33.1', '::', '::', '192.168.1.6', '192.168.1.6', '192.168.1.6', '192.168.1.6', '0.0.0.0', '192.168.1.6', '::', '0.0.0.0', '192.168.1.6', '192.168.1.6', '192.168.0.131', '::', '192.168.0.131', '192.168.1.6', '192.168.1.6', '192.168.1.6', '0.0.0.0']
...and then with those somehow figure out what the NIC those connections are referring to may be.
Considering we can get a list of NIC names + associated addresses with psutil.net_if_addrs() I came up with this.
It works for IPv4 connections only and it probably is not reliable but here goes.
import psutil
import socket
from collections import defaultdict
def get_addrs_map():
addrs_map = defaultdict(list)
for nic, addrs in psutil.net_if_addrs().items():
for addr in addrs:
if addr.family == socket.AF_INET:
first3octs = '.'.join(addr.address.split('.')[:3])
addrs_map[first3octs].append(nic)
return addrs_map
ret = defaultdict(int)
addrs_map = get_addrs_map()
local_addrs = [x.laddr[0] for x in psutil.net_connections(kind='inet4')]
for addr in local_addrs:
first3octs = '.'.join(addr.split('.')[:3])
try:
nics = addrs_map[first3octs]
except KeyError:
ret['unknown'] += 1
else:
for nic in nics:
ret[nic] += 1
print dict(ret)