pypi i libnl


Pure Python port of the Netlink protocol library suite.

by Robpol86

0.2.0 (see all)License:LGPL2.1
pypi i libnl



A port of libnl <>_, a collection of libraries providing APIs to the Netlink protocol based Linux kernel interfaces. This library is API-equivalent to the original C library, and should make it relatively easy to convert C programs into pure Python without having to call external binaries.

As Netlink is a Linux-specific protocol, this library will only work on Linux hosts. All communication is done using sockets between the Python process and the Linux kernel. The main driver for porting libnl was to use nl80211 <>_ in Python to scan for wireless access points natively, without having to run an external program and parse its output.

  • Python 2.6, 2.7, PyPy, PyPy3, 3.3, and 3.4 supported on Linux

.. |buildWercker| image:: :target: :alt: Build Status WiFi

.. |buildTravis| image:: :target: :alt: Build Status

.. |coverage| image:: :target: :alt: Coverage Status

.. |latestVersion| image:: :target: :alt: Latest Version

.. |downloads| image:: :target: :alt: Downloads

============== ================ ============= =============== =========== WiFi Linux Coverage Latest Downloads ============== ================ ============= =============== =========== |buildWercker| |buildTravis| |coverage| |latestVersion| |downloads| ============== ================ ============= =============== ===========



.. code:: bash

pip install libnl

Example Implementations_

A simple Python program that merely lists network adapters on the host:

.. code:: python

import ctypes
import socket

from libnl.error import errmsg
from libnl.handlers import NL_CB_CUSTOM, NL_CB_VALID, NL_OK
from libnl.linux_private.if_link import IFLA_IFNAME, IFLA_RTA
from libnl.linux_private.netlink import NETLINK_ROUTE, NLMSG_LENGTH, NLM_F_DUMP, NLM_F_REQUEST
from libnl.linux_private.rtnetlink import RTA_DATA, RTA_NEXT, RTA_OK, RTM_GETLINK, ifinfomsg, rtgenmsg
from libnl.misc import get_string
from libnl.msg import nlmsg_data, nlmsg_hdr
from import nl_connect, nl_recvmsgs_default, nl_send_simple
from libnl.socket_ import nl_socket_alloc, nl_socket_modify_cb

def callback(msg, _):
    nlh = nlmsg_hdr(msg)
    iface = ifinfomsg(nlmsg_data(nlh))
    hdr = IFLA_RTA(iface)
    remaining = ctypes.c_int(nlh.nlmsg_len - NLMSG_LENGTH(iface.SIZEOF))
    while RTA_OK(hdr, remaining):
        if hdr.rta_type == IFLA_IFNAME:
            print('Found interface {0}: {1}'.format(iface.ifi_index, get_string(RTA_DATA(hdr)).decode('ascii')))
        hdr = RTA_NEXT(hdr, remaining)
    return NL_OK

sk = nl_socket_alloc()  # Creates an nl_sock instance.
ret = nl_connect(sk, NETLINK_ROUTE)  # Create file descriptor and bind socket.
if ret < 0:
    raise RuntimeError('nl_connect() returned {0} ({1})'.format(ret, errmsg[abs(ret)]))
rt_hdr = rtgenmsg(rtgen_family=socket.AF_PACKET)
ret = nl_send_simple(sk, RTM_GETLINK, NLM_F_REQUEST | NLM_F_DUMP, rt_hdr, rt_hdr.SIZEOF)
if ret < 0:
    raise RuntimeError('nl_send_simple() returned {0} ({1})'.format(ret, errmsg[abs(ret)]))
nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, callback, None)  # Add callback to the nl_sock instance.
ret = nl_recvmsgs_default(sk)  # Get kernel's answer, and call attached callbacks.
if ret < 0:
    raise RuntimeError('nl_recvmsgs_default() returned {0} ({1})'.format(ret, errmsg[abs(ret)]))

Here are some more examples with their C equivalents in order from "easy" to "hard":

  • <> (list_network_interfaces.c <>)
  • <> (show_wifi_interface.c <>)
  • <> (scan_access_points.c <>)


This project adheres to Semantic Versioning <>_.

0.2.0 - 2015-03-26_


* Python2.6, PyPy, and PyPy3 support.

0.1.1 - 2015-03-15_

  • Initial release.
8yrs ago
8yrs ago
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

No reviews found
Be the first to rate