If you don't care about anything other than linux, you could use the pyroute2
module. For example, to get the routing information for a particular IP address:
>>> import pprint
>>> import pyroute2
>>> import socket
>>> ip = pyroute2.IPRoute()
>>> pprint.pprint(ip.get_routes(family=socket.AF_INET, dst='127.0.0.6'))
[{'attrs': [['RTA_TABLE', 254],
['RTA_DST', '127.0.0.6'],
['RTA_OIF', 1],
['RTA_PREFSRC', '127.0.0.1'],
['RTA_CACHEINFO',
{'rta_clntref': 1,
'rta_error': 0,
'rta_expires': 0,
'rta_id': 0,
'rta_lastuse': 0,
'rta_ts': 0,
'rta_tsage': 0,
'rta_used': 1}]],
'dst_len': 32,
'event': 'RTM_NEWROUTE',
'family': 2,
'flags': 2147484160,
'proto': 0,
'scope': 0,
'src_len': 0,
'table': 254,
'tos': 0,
'type': 2}]
Unfortunately, this doesn't work with the latest version of pyroute2
that's on pypi; I had to install from source in order to get these results.