我有帶testFirewallS8類的S8Test.py類和一些方法。我想訪問在這個類中聲明的方法形式的主要方法。並從該方法設置變量另一個python文件包含相同的變量進行修改。我怎樣才能做到這一點:如何從main訪問python類的方法並在另一個python文件中傳遞和訪問變量
#! /usr/bin/env python
__author__ = 'Milson Munakami'
__revision__ = '0.0.2'
import json
import urllib
import httplib
from scapy.all import *
import unittest
import os, sys, socket, struct, select, time
from threading import Thread
import logging
import traceback
from mininet.net import Mininet
from mininet.node import OVSSwitch, OVSKernelSwitch, Controller, RemoteController
from mininet.log import setLogLevel, info
from mininet.cli import CLI
class TestFirewallS8(unittest.TestCase):
def setUp(self):
self.controllerIp="127.0.0.1"
self.switch = "00:00:00:00:00:00:00:01"
self.destinationIp = "10.0.0.1"
self.startTime_ = time.time()
self.failed = False
self.reportStatus_ = True
self.name_ = "Firewall"
self.log = logging.getLogger("unittest")
"Create an empty network and add nodes to it."
self.net = Mininet(controller=RemoteController)
#Want to move this method call from outside the setUp method because it need to be initiated only once for the whole test but
#it need to access the class variables and pass it to another python file i.e. Events.py to perform some task on the object i.e. self
#self.CreateNet()
def createNet(self):
print "Me"
info('*** Adding controller\n')
self.net.addController('c0' , controller=RemoteController,ip= "127.0.0.1", port=6633)
info('*** Adding hosts\n')
h1 = self.net.addHost('h1', ip='10.0.0.1')
h2 = self.net.addHost('h2', ip='10.0.0.2')
h3 = self.net.addHost('h3', ip='10.0.0.3')
info('*** Adding switch\n')
s1 = self.net.addSwitch('s1')
info('*** Creating links\n')
self.net.addLink(h1, s1)
self.net.addLink(h2, s1)
self.net.addLink(h3, s1)
def setFinalcondition(self):
Precondition.SetFinalcondition(self)
info('*** Stopping network')
self.net.stop()
def testCreateFlow(self):
Events.createFlow(self)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestFirewallS8))
return suite
if __name__ == '__main__':
#How to get run the method of testFirewallS8 class and set the variable of it like self.net
suiteFew = unittest.TestSuite(testCreateFlow)
TestFirewallS8("createNet")
在另一個Events.py我:
def createFlow(self):
info('*** Starting network\n')
self.net.start()
info('*** Testing network connecivity\n')
#self.net.pingAll()
h1 = self.net.get('h1')
h3 = self.net.get('h3')
h1.cmd('ping -c1 %s' % h3.IP())
請按照使用'UpperCase' Python的約定和名稱類的,否則他們是容易的樣子功能。 – 2014-10-03 22:39:53
根據你的描述,不清楚它的意圖是什麼或問題是什麼。您的示例代碼將受益於[如何創建一個最小化,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – wwii 2014-10-03 23:12:37