Fix all Python import ordering
[integration/packaging.git] / tutorials / l2switch / mininet.py
1 #!/usr/bin/env python
2 """Creates a Mininet topology based on examples in the main Mininet documentation."""
3
4 import time
5
6 from mininet.log import setLogLevel
7 from mininet.net import Mininet
8 from mininet.node import RemoteController
9 from mininet.topo import Topo
10
11
12 class SingleSwitchTopo(Topo):
13     """Single switch connected to n hosts."""
14
15     def build(self, n=2):
16         switch = self.addSwitch("s1")
17         # Python's range(N) generates 0..N-1
18         for h in range(n):
19             host = self.addHost("h%s" % (h + 1))
20             self.addLink(host, switch)
21
22
23 def simple_test():
24     """Create and test a simple network."""
25     topo = SingleSwitchTopo(n=4)
26     net = Mininet(topo=topo, controller=None)
27     net.addController("c0", controller=RemoteController,
28                       ip="127.0.0.1", port=6633)
29     net.start()
30     time.sleep(10)
31     print "Testing network connectivity"
32     net.pingAll()
33
34
35 if __name__ == "__main__":
36     # Tell mininet to print useful information
37     setLogLevel("info")
38     simple_test()