Skip to main content

Linux TUN/TAP wrapper for Python

Project description

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 setup.py 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.

Documentation

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:

print tun.name
tun.addr = '10.8.0.1'
tun.dstaddr = '10.8.0.2'
tun.netmask = '255.255.255.0'
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:

tun.persist(True)

To bring up the device:

tun.up()

To bring down the device:

tun.down()

To enable/disable the queue associated with the device (works only if it has been created with IFF_MULTI_QUEUE):

tun.mq_attach() # enable the queue
tun.mq_attach(False) # disable the queue

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

buf = tun.read(tun.mtu)
tun.write(buf)

To close the device:

tun.close()

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-pytun-2.4.1.tar.gz (8.5 kB view hashes)

Uploaded Source

Built Distributions

python_pytun-2.4.1-cp311-cp311-musllinux_1_1_x86_64.whl (32.2 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

python_pytun-2.4.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (29.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_pytun-2.4.1-cp310-cp310-musllinux_1_1_x86_64.whl (31.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

python_pytun-2.4.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (28.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_pytun-2.4.1-cp39-cp39-musllinux_1_1_x86_64.whl (30.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

python_pytun-2.4.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (27.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_pytun-2.4.1-cp39-cp39-manylinux2014_x86_64.whl (31.9 kB view hashes)

Uploaded CPython 3.9

python_pytun-2.4.1-cp39-cp39-manylinux1_x86_64.whl (31.2 kB view hashes)

Uploaded CPython 3.9

python_pytun-2.4.1-cp38-cp38-musllinux_1_1_x86_64.whl (31.5 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

python_pytun-2.4.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (28.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_pytun-2.4.1-cp38-cp38-manylinux2014_x86_64.whl (32.4 kB view hashes)

Uploaded CPython 3.8

python_pytun-2.4.1-cp38-cp38-manylinux1_x86_64.whl (31.7 kB view hashes)

Uploaded CPython 3.8

python_pytun-2.4.1-cp37-cp37m-musllinux_1_1_x86_64.whl (31.7 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

python_pytun-2.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (29.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_pytun-2.4.1-cp37-cp37m-manylinux2014_x86_64.whl (32.7 kB view hashes)

Uploaded CPython 3.7m

python_pytun-2.4.1-cp37-cp37m-manylinux1_x86_64.whl (31.9 kB view hashes)

Uploaded CPython 3.7m

python_pytun-2.4.1-cp36-cp36m-musllinux_1_1_x86_64.whl (30.7 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

python_pytun-2.4.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (28.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_pytun-2.4.1-cp36-cp36m-manylinux2014_x86_64.whl (31.8 kB view hashes)

Uploaded CPython 3.6m

python_pytun-2.4.1-cp36-cp36m-manylinux1_x86_64.whl (31.0 kB view hashes)

Uploaded CPython 3.6m

python_pytun-2.4.1-cp35-cp35m-manylinux2014_x86_64.whl (31.5 kB view hashes)

Uploaded CPython 3.5m

python_pytun-2.4.1-cp35-cp35m-manylinux2010_x86_64.whl (31.0 kB view hashes)

Uploaded CPython 3.5m manylinux: glibc 2.12+ x86-64

python_pytun-2.4.1-cp35-cp35m-manylinux1_x86_64.whl (30.9 kB view hashes)

Uploaded CPython 3.5m

python_pytun-2.4.1-cp27-cp27mu-manylinux2010_x86_64.whl (29.1 kB view hashes)

Uploaded CPython 2.7mu manylinux: glibc 2.12+ x86-64

python_pytun-2.4.1-cp27-cp27mu-manylinux1_x86_64.whl (29.6 kB view hashes)

Uploaded CPython 2.7mu

python_pytun-2.4.1-cp27-cp27m-manylinux2010_x86_64.whl (29.1 kB view hashes)

Uploaded CPython 2.7m manylinux: glibc 2.12+ x86-64

python_pytun-2.4.1-cp27-cp27m-manylinux1_x86_64.whl (29.6 kB view hashes)

Uploaded CPython 2.7m

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page