python - 如何在 python 中运行 bash 'tc' 命令?

标签 python linux trafficshaping

我想用 Python 对 Linux 内核进行流量控制,以模拟丢失、损坏和重复的包。我已经能够使用 Linux 终端进行配置,但我必须使用 python。

bash cmd 工作:

tc filter show dev eth1

python 不工作:

>>> subprocess.call(["tc", "filter", "show", "dev", "eth1"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/subprocess.py", line 470, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

谢谢。

最佳答案

python 子进程不知道您的 shell 环境。因此,为您的命令提供绝对路径,例如:

subprocess.call(["/sbin/tc", "filter", "show", "dev", "eth1"])

在您的 shell 中使用命令 which tc 查找确切位置。

关于python - 如何在 python 中运行 bash 'tc' 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25287125/

相关文章:

linux - 如何 tar 目录中的所有文件并将该 tar 移动到另一个目录

trafficshaping - Linux 中的 TC(流量控制)

linux - 是否可以对 iproute tc 命令进行逆向工程?

python - Django查询集模型在字段中应用函数

linux - En_US 和 en_US 之间的区别?

python - Scikit 学习,Numpy : Changing the value of an read-only variable

linux - 重定向 - Shell 脚本

linux - 如何使用tc限制网络速度?

python - BeautifulSoup属性错误: 'NoneType' object has no attribute 'text'

python - 将 Django 命令分组到同一应用程序内的文件夹中