new1.py for 3 path and 2 host

#!/usr/bin/python
import sys
sys.path.append('/home/openlab/mininet/')
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call
import threading
import time

def myNetwork():
    net = Mininet( topo=None,controller=None,ipBase='10.0.0.0/8')
    
    ## specify clearly to use a remote controller instead of the default one 
    c=RemoteController('c','0.0.0.0',6633)
    net.addController(c)

    info( '*** Add switches\n')
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
    s3 = net.addSwitch('s3', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
    s4 = net.addSwitch('s4', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
    s5 = net.addSwitch('s5', cls=OVSKernelSwitch,protocols=['OpenFlow13'])

    s6 = net.addSwitch('s6', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
    s7 = net.addSwitch('s7', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
    s8 = net.addSwitch('s8', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
    s9 = net.addSwitch('s9', cls=OVSKernelSwitch,protocols=['OpenFlow13'])
 
    info( '*** Add hosts\n')
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None,mac='00:00:00:00:00:01')
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None,mac='00:00:00:00:00:02')

    
    info( '*** Add links\n')
    net.addLink(h1, s1)
    net.addLink(h2, s5)

    net.addLink(s1, s2,bw=10, delay='1ms')
    net.addLink(s1, s4,bw=10, delay='1ms')
    #net.addLink(s2, s3,bw=10, delay='1ms')
    #net.addLink(s4, s5,bw=10, delay='1ms')
    net.addLink(s3, s5,bw=10, delay='1ms')

    net.addLink(s1, s6,bw=10, delay='1ms')
    net.addLink(s6, s7,bw=10, delay='1ms')
    net.addLink(s7, s8,bw=10, delay='1ms')
    net.addLink(s8, s9,bw=10, delay='1ms')
    net.addLink(s5, s9,bw=10, delay='1ms')


    ## start switch
    for i in range(1,10):
        net.get("s{}".format(i)).start([])

    info( '*** Starting network\n')
    net.start()

    #into the interactive mode
    def thread1():
        CLI(net)
        net.stop()

    t1 = threading.Thread(target = thread1)
    t1.start()
    def thread2():#kill s1 s4;s2 s3
        time.sleep(15)
        j=0
        k=3
        net.configLinkStatus('s' + str(int(j+1)),'s' + str(int(k+1)),'down')
        net.configLinkStatus('s' + str(int(2)),'s' + str(int(2+1)),'down')        

    t2 = threading.Thread(target = thread2)
    t2.start()

    def thread3():#up s1 s4;s2 s3
        time.sleep(30)
        j=0
        k=3
        net.configLinkStatus('s' + str(int(j+1)),'s' + str(int(k+1)),'up')
        net.configLinkStatus('s' + str(int(2)),'s' + str(int(2+1)),'up')
        #add()    

    t3 = threading.Thread(target = thread3)
    t3.start()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

抱歉,我在之前的回答中漏掉了一些重要的部分。以下是修复错误的更新步骤: 1. 首先,确保你已经安装了`channels`库: ``` pip install channels ``` 2. 在你的Django项目的`settings.py`文件中添加`channels`应用到`INSTALLED_APPS`列表中: ```python INSTALLED_APPS = [ ... 'channels', ] ``` 3. 创建一个消费者(consumer)文件,例如`consumers.py`,并添加以下内容: ```python from channels.generic.websocket import AsyncWebsocketConsumer class PrintConsumer(AsyncWebsocketConsumer): async def connect(self): await self.accept() async def disconnect(self, close_code): pass async def receive(self, text_data): # 这里可以处理接收到的WebSocket消息,例如将其打印出来 print(text_data) await self.send(text_data) ``` 4. 在你的Django项目中创建一个路由(routing)文件,例如`routing.py`,并添加以下内容: ```python from django.urls import re_path from . import consumers websocket_urlpatterns = [ re_path(r'ws/print/$', consumers.PrintConsumer.as_asgi()), ] ``` 5. 在你的Django项目的主目录下创建一个`asgi.py`文件,并添加以下内容: ```python import os from django.core.asgi import get_asgi_application from channels.routing import ProtocolTypeRouter, URLRouter from . import routing os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project_name.settings') application = ProtocolTypeRouter({ 'http': get_asgi_application(), 'websocket': URLRouter(routing.websocket_urlpatterns), }) ``` 6. 在你的网页中添加JavaScript代码,用于创建WebSocket连接和处理消息的逻辑: ```javascript var ws = new WebSocket('ws://' + window.location.host + '/ws/print/'); ws.onmessage = function(event) { var message = event.data; // 在这里处理接收到的消息,例如在网页弹窗中显示 alert(message); }; ``` 7. 最后,在你的Python代码中,将标准输出重定向到一个StringIO对象,以便将print输出的内容发送到WebSocket: ```python import sys from io import StringIO sys.stdout = StringIO() print("Hello, world!") # 这个输出将会通过WebSocket发送到网页弹窗中 ``` 请确保按照以上步骤进行操作,并替换`your_project_name`为你的Django项目名称。这样就可以实现将Python中的print输出内容实时显示在网页弹窗中了。如果还有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值