pyroute2 0.9.7


pip install pyroute2

  Latest version

Released: Oct 23, 2026

Project Links

Meta
Author: Peter Saveliev
Requires Python: >=3.9

Classifiers

Programming Language
  • Python :: 3
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: 3.14

Topic
  • Software Development :: Libraries :: Python Modules
  • System :: Networking
  • System :: Systems Administration

Operating System
  • POSIX :: Linux

Intended Audience
  • Developers
  • System Administrators
  • Telecommunications Industry

Development Status
  • 4 - Beta

Pyroute2 is a pure Python networking framework. The core requires only Python stdlib, no 3rd party libraries. The library was started as an RTNL protocol implementation, so the name is pyroute2, but now it supports several protocols, including non-netlink. Here are some supported netlink families and protocols:

  • dhcp — dynamic host configuration protocol for IPv4

  • 9p2000 — Plan9 file system protocol

Netlink:

  • rtnl, network settings — addresses, routes, traffic controls

  • nfnetlink — netfilter API

  • ipq — simplest userspace packet filtering, iptables QUEUE target

  • devlink — manage and monitor devlink-enabled hardware

  • generic — generic netlink families

  • uevent — same uevent messages as in udev

Netfilter API:

  • ipset — IP sets

  • nftables — packet filtering

  • nfct — connection tracking

Generic netlink:

  • ethtool — low-level network interface setup

  • wireguard — VPN setup

  • nl80211 — wireless functions API (basic support)

  • taskstats — extended process statistics

  • acpi_events — ACPI events monitoring

  • thermal_events — thermal events monitoring

  • VFS_DQUOT — disk quota events monitoring

On the low level the library provides socket objects with an extended API. The additional functionality aims to:

  • Help to open/bind netlink sockets

  • Discover generic netlink protocols and multicast groups

  • Construct, encode and decode netlink and PF_ROUTE messages

Supported systems

Pyroute2 runs natively on Linux and emulates some limited subset of RTNL netlink API on BSD systems on top of PF_ROUTE notifications and standard system tools.

Other platforms are not supported.

IPRoute – synchronous RTNL API

Low-level IPRoute utility — Linux network configuration, this class is almost a 1-to-1 RTNL mapping. There are no implicit interface lookups and so on.

Get notifications about network settings changes:

from pyroute2 import IPRoute

with IPRoute() as ipr:
    ipr.bind()  # <--- start listening for RTNL broadcasts
    for message in ipr.get():  # receive the broadcasts
        print(message)

More examples:

from socket import AF_INET
from pyroute2 import IPRoute

# get access to the netlink socket
ipr = IPRoute()
# no monitoring here -- thus no bind()

# print interfaces
for link in ipr.get_links():
    print(link)

# create VETH pair and move v0p1 to netns 'test'
ipr.link('add', ifname='v0p0', peer='v0p1', kind='veth')
# wait for the devices:
peer, veth = ipr.poll(
    ipr.link, 'dump', timeout=5, ifname=lambda x: x in ('v0p0', 'v0p1')
)
ipr.link('set', index=peer['index'], net_ns_fd='test')

# bring v0p0 up and add an address
ipr.link('set', index=veth['index'], state='up')
ipr.addr('add', index=veth['index'], address='10.0.0.1', prefixlen=24)

# release Netlink socket
ipr.close()

AsyncIPRoute – asynchronous RTNL API

While IPRoute provides a synchronous RTNL API, it is actually build around the asyncio-based core.

The same example as above can look like that:

import asyncio

from pyroute2 import AsyncIPRoute

async def main():
    # get access to the netlink socket
    ipr = AsyncIPRoute()

    # print interfaces
    async for link in await ipr.get_links():
        print(link)

    # create VETH pair and move v0p1 to netns 'test'
    await ipr.link('add', ifname='v0p0', peer='v0p1', kind='veth')

    # wait for the devices:
    peer, veth = await ipr.poll(
        ipr.link, 'dump', timeout=5, ifname=lambda x: x in ('v0p0', 'v0p1')
    )
    await ipr.link('set', index=peer['index'], net_ns_fd='test')
    ...
    ipr.close()

 asyncio.run(main())

Please notice that .close() is synchronous in any case.

Network namespace management

from pyroute2 import netns
# create netns
netns.create('test')
# list
print(netns.listnetns())
# remove netns
netns.remove('test')

Create veth interfaces pair and move to netns:

from pyroute2 import IPRoute

with IPRoute() as ipr:

    # create interface pair
    ipr.link('add', ifname='v0p0', kind='veth',  peer='v0p1')

    # wait for the peer
    (peer,) = ipr.poll(ipr.link, 'dump', timeout=5, ifname='v0p1')

    # move the peer to the 'test' netns:
    ipr.link('set', index=peer['index'], net_ns_fd='test')

List interfaces in some netns:

from pyroute2 import IPRoute

with IPRoute(netns='test') as ipr:
    for link in ipr.get_links():
        print(link)

More details and samples see in the documentation.

NDB – high level RTNL API

Key features:

  • Data integrity

  • Transactions with commit/rollback changes

  • State synchronization

  • Multiple sources, including netns and remote systems

A “Hello world” example:

from pyroute2 import NDB

with NDB() as ndb:
    with ndb.interfaces['eth0'] as eth0:
        # set one parameter
        eth0.set(state='down')
        eth0.commit()  # make sure that the interface is down
        # or multiple parameters at once
        eth0.set(ifname='hello_world!', state='up')
        eth0.commit()  # rename, bring up and wait for success
    # --> <-- here you can be sure that the interface is up & renamed

Installation

Using pypi:

pip install pyroute2

Using git:

pip install git+https://github.com/svinota/pyroute2.git

Using source, requires make and nox

git clone https://github.com/svinota/pyroute2.git
cd pyroute2
make install

Requirements

Python >= 3.10

0.9.7 Oct 23, 2026
0.9.6 Apr 15, 2026
0.9.5 Nov 02, 2025
0.9.4 Jul 29, 2025
0.9.3 Jul 23, 2025
0.9.3rc2 Jul 21, 2025
0.9.3rc1 Jul 18, 2025
0.9.2 Apr 28, 2025
0.9.1 Apr 02, 2025
0.9.1rc2 Mar 27, 2025
0.9.1rc1 Mar 16, 2025
0.8.1 Dec 20, 2024
0.7.12 Feb 10, 2024
0.7.11 Jan 12, 2024
0.7.10 Nov 30, 2023
0.7.9 May 31, 2023
0.7.8 May 05, 2023
0.7.7 Apr 14, 2023
0.7.6 Mar 19, 2023
0.7.5 Feb 12, 2023
0.7.4 Jan 24, 2023
0.7.3 Sep 09, 2022
0.7.2 Jul 19, 2022
0.7.1 Jul 13, 2022
0.6.13 Jun 23, 2022
0.6.12 Jun 12, 2022
0.6.11 Jun 06, 2022
0.6.10 Jun 06, 2022
0.6.9 Mar 21, 2022
0.6.8 Mar 11, 2022
0.6.7 Feb 21, 2022
0.6.6 Feb 16, 2022
0.6.5 Sep 20, 2021
0.6.4 Jun 06, 2021
0.6.3 May 31, 2021
0.6.2 May 25, 2021
0.6.1 May 18, 2021
0.5.19 May 13, 2021
0.5.18 Apr 12, 2021
0.5.17 Apr 04, 2021
0.5.16 Mar 29, 2021
0.5.15 Mar 26, 2021
0.5.14 Sep 09, 2020
0.5.13 Jul 27, 2020
0.5.12 Apr 17, 2020
0.5.11 Mar 23, 2020
0.5.10 Mar 12, 2020
0.5.9 Feb 13, 2020
0.5.8 Feb 13, 2020
0.5.7 Oct 14, 2019
0.5.6 May 26, 2019
0.5.5 Mar 29, 2019
0.5.4 Mar 03, 2019
0.5.3 Sep 30, 2018
0.5.2 Jun 19, 2018
0.5.1 May 23, 2018
0.5.0 Mar 22, 2018
0.4.21 Sep 01, 2017
0.4.20 Aug 30, 2017
0.4.19 Aug 01, 2017
0.4.18 Jul 10, 2017
0.4.17 Jun 23, 2017
0.4.16 Jun 14, 2017
0.4.15 May 31, 2017
0.4.14 May 20, 2017
0.4.13 Feb 18, 2017
0.4.12 Jan 06, 2017
0.4.11 Nov 18, 2016
0.4.10 Oct 14, 2016
0.4.9 Oct 06, 2016
0.4.8 Sep 24, 2016
0.4.7 Sep 23, 2016
0.4.6 Aug 18, 2016
0.4.5 Aug 01, 2016
0.4.4 Jun 30, 2016
0.4.3 Jun 16, 2016
0.4.2 Jun 08, 2016
0.4.1 May 21, 2016
0.4.0 May 07, 2016
0.3.22 Apr 27, 2016
0.3.21 Apr 12, 2016
0.3.20 Apr 09, 2016
0.3.19 Apr 07, 2016
0.3.18 Apr 04, 2016
0.3.16 Feb 26, 2016
0.3.15 Nov 18, 2015
0.3.14 Sep 01, 2015
0.3.13 Sep 01, 2015
0.3.12 Aug 24, 2015
0.3.11 Jul 31, 2015
0.3.10 Jul 28, 2015
0.3.9 Jun 01, 2015
0.3.8 May 06, 2015
0.3.7 Apr 27, 2015
0.3.6 Mar 28, 2015
0.3.5 Feb 28, 2015
0.3.4 Jan 08, 2015
0.3.3 Dec 05, 2014
0.3.2 Nov 19, 2014
0.3.1 Oct 11, 2014
0.2.16 Sep 30, 2014
0.2.14 Aug 24, 2014
0.2.13 Aug 10, 2014
0.2.12 Jul 17, 2014
0.2.11 May 23, 2014
0.2.10 May 12, 2014
0.2.9 May 02, 2014
0.2.8 Apr 11, 2014
0.2.7 Mar 12, 2014
0.2.6 Feb 09, 2014
0.2.4 Jan 04, 2014
0.2.3 Jan 03, 2014
0.2.2 Jan 03, 2014
0.1.12 Sep 09, 2013
0.1.10 Jun 28, 2013
0.1.9 Jun 18, 2013
0.1.7 May 29, 2013
0.1.6 May 28, 2013
0.1.5 May 14, 2013
0.1.4 May 05, 2013
0.1.3 Apr 30, 2013
0.1.2 Apr 28, 2013
0.1.1 Apr 26, 2013

Wheel compatibility matrix

Platform Python 3
any

Files in release