# 自定义拓扑

Mininet 提供了 Python API，可以用来方便的自定义拓扑结构。

在 mininet/custom 目录下给出了几个例子。例如在 topo-2sw-2host.py 文件中定义了一个 mytopo，则可以通过 `--topo` 选项来指定使用这一拓扑，命令为 `sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --topo mytopo --test pingall`。

同样的，我们可以通过下面的 Python 脚本来完成对一个2 层 tree 拓扑网络的测试。

```
from mininet.net import Mininet
from mininet.topolib import TreeTopo
tree4 = TreeTopo(depth=2,fanout=2)
net = Mininet(topo=tree4)
net.start()
h1, h4 = net.hosts[0], net.hosts[3]
print h1.cmd('ping -c1 %s' % h4.IP())
net.stop()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/mininet_book/operation/topology.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
