Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Linux TUN/TAP wrapper for Python
C Python
Branch: master


Linux TUN/TAP wrapper for Python

pytun is a Python module which let you create TUN/TAP device very easily.

License: MIT (see LICENSE)

Installation and Dependencies

Install pytun with pip install python-pytun or download this archive, decompress it and execute python install. As pytun is a C module you will need a compiler (e.g GCC) and the Python developpement headers installed on your system (e.g on Debian-like distribution check that build-essential and python-dev are present). There are no dependencies other than the Python Standard Library.


NOTE: On most distributions you will need to be root to create TUN/TAP devices.

To create a TUN device:

from pytun import TunTapDevice

tun = TunTapDevice()

To create a TAP device:

from pytun import TunTapDevice, IFF_TAP

tap = TunTapDevice(flags=IFF_TAP)

To create a TUN/TAP device with a custom name use the name keyword:

tun = TunTapDevice(name='mytun')

You can get/set some parameters of the device directly:

tun.addr = ''
tun.dstaddr = ''
tun.netmask = ''
tun.mtu = 1500

If the device is a TAP you can also get/set its MAC address:

tap.hwaddr = '\x00\x11\x22\x33\x44\x55'
print tap.hwaddr

To make the device persistent:


To bring up the device:


To bring down the device:


To read/write to the device, use the methods read(size) and write(buf):

buf =

To close the device:


You can also use TunTapDevice objects with all functions that expect a fileno() method (e.g select())

Something went wrong with that request. Please try again.