百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pytun

> 编程语言
开源

用于 Python 的 Linux TUN/TAP 封装程序

179 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

用于 Python 的 Linux TUN/TAP 封装程序

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 setup.py install. As pytun is a C module you will need a compiler (e.g GCC) and the Python development 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())

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ccnetworkingpython2python3

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言