示例#1
0
class HandleMysql:
    def __init__(self):
        self.data = ReadConfig()

    def conn_mysql(self):
        """连接数据库"""
        host = self.data.get_db("mysql", "host")
        user = self.data.get_db("mysql", "user")
        password = self.data.get_db("mysql", "passwd")
        db = self.data.get_db("mysql", "db")
        charset = self.data.get_db("mysql", "charset")
        self.conn = pymysql.connect(host=host,
                                    user=user,
                                    password=password,
                                    db=db,
                                    charset=charset)
        self.cur = self.conn.cursor()

    def execute_sql(self, sql, data):
        """执行操作数据的相关sql"""
        self.conn_mysql()
        self.cur.execute(sql, data)
        self.conn.commit()

    def search(self, sql):
        """执行查询sql"""
        self.conn_mysql()
        self.cur.execute(sql)
        result = self.cur.fetchall()
        self.conn.commit()
        return result

    def dispose(self):
        self.cur.close()
        self.conn.close()
示例#2
0
def test_MySQLWrapper(number=100):
    cfg = ReadConfig('./db.info')
    cfgd = cfg.check_config()
    sop = cfgd['mysql']['app_eemsop']
    print('sop', sop)
    ####
    try:
        sql_pool1 = MySQLWrapper(**sop)
    except:
        print('error')
        return -1
    time1_start = time.time()
    for i in range(int(number)):
        sql = 'select * from users where id=%s' % (int(random.random() * 5))
        res = sql_pool1.do_work(sql)
    print(res)
    time1_end = time.time()
    time1_spend = time1_end - time1_start
    print('my own mysql wraper, query  times %s, spend %s s' %
          (number, time1_spend))
    time2_start = time.time()
    for i in range(int(number)):
        conn = MySQLdb.connect(**sop)
        c = conn.cursor()
        c.execute('select * from users where id=%s' %
                  (int(random.random() * 5)))
        res = c.fetchall()
        conn.close()
    time2_end = time.time()
    time2_spend = time2_end - time1_start
    print('normal, query  times %s, spend %s s' % (number, time2_spend))
    return [time1_spend, time2_spend]
示例#3
0
 def __init__(self):
     """
     Constructor to initialise credentials.
     """
     self.config = ReadConfig()
     self.access_token = self.config.get_access_token()
     self.access_secret = self.config.get_access_secret()
     self.consumer_key = self.config.get_consumer_key()
     self.consumer_secret = self.config.get_consumer_secret()
示例#4
0
def main():
    print('db_pool is a database connection pool, as tool class.')
    cfg = ReadConfig('./db.info')
    cfgd = cfg.check_config()
    rp = RedisWrapper(host=cfgd['redis']['91']['host'])
    r1 = rp.redis_connect()
    rp2 = redis.ConnectionPool(host='172.16.5.91', port=6379)
    r2 = redis.Redis(connection_pool=rp2)
    print(r1)
    print(r2)
    test_MySQLWrapper()
示例#5
0
    def test_get_access_token(self, mock_open, mock_yaml):
        mock_open.return_value.__enter__.return_value = True
        mock_yaml.safe_load.return_value = {
            'development': {
                'access_token': 'my_access_token'
            }
        }

        r = ReadConfig()
        actual_result = r.get_access_token()

        self.assertEqual(actual_result, 'my_access_token')
示例#6
0
    def test_get_consumer_key(self, mock_open, mock_yaml):
        mock_open.return_value.__enter__.return_value = True
        mock_yaml.safe_load.return_value = {
            'development': {
                'consumer_key': 'my_consumer_key'
            }
        }

        r = ReadConfig()
        actual_result = r.get_consumer_key()

        self.assertEqual(actual_result, 'my_consumer_key')
示例#7
0
def rd(server='91'):
    cfg = ReadConfig('./db.info')
    cfgd = cfg.check_config()
    try:
        host = '6379' if not ('host' in cfgd['redis'][server].keys()
                              ) else cfgd['redis'][server]['host']
        port = '6379' if not ('port' in cfgd['redis'][server].keys()
                              ) else cfgd['redis'][server]['port']
        db = '0' if not ('db' in cfgd['redis'][server].keys()
                         ) else cfgd['redis'][server]['db']
        # print(host, port, db)
        rp = RedisWrapper(host=host, port=port, db=db, name=server)
    except:
        return None
    return rp.redis_connect()
示例#8
0
  def start_download(self):
    tracker_ip = "http://{}/announce".format(ReadFile.getTrackerIp(self.file_path))    
    hostname = socket.gethostname()    
    myip = socket.gethostbyname(hostname)    
    peerId = ReadConfig.getPeerId(CONFIG_PATH)    
    info_hash = ReadFile.getInfoHash(self.file_path)
    port = ReadConfig.getPort(CONFIG_PATH)

    peers = Peers.getPeers(tracker_ip, info_hash, myip, port, 0, peerId)

    piece_length = ReadFile.getPieceLength(self.file_path)
    q = self.getRemainingPieces()

    for p in peers:
      if(q.empty()):
        break
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect((p['ip'], int(p['port'])))
      handshake = Messages.handshake(info_hash, peerId)
      s.send(handshake.encode())
      data = s.recv(piece_length)
      if(data != b'ok'):
        s.close()
        continue
      cnt = 0
      while not q.empty():
        elem = q.get()
        msg = '0001{}'.format(str(elem))
        s.send(msg.encode())
        data = s.recv(piece_length)
        if(self.verifyHash(data, elem)):
          self.saveFile(data.decode('utf-8'), elem)
          cnt = 0
        else:
          q.put(elem)
          cnt = cnt + 1
        if cnt == 10:
          break
      s.send(b"0002")
      data = s.recv(piece_length)
      s.close()

    temp = "{}{}".format(os.path.splitext(self.output_path)[0], "/temp")
    ConcatenateFile.concatenateFile(self.file_path, self.output_path, temp)
    temp = os.path.splitext(self.output_path)[0]
    shutil.rmtree(temp)
示例#9
0
class GetCookie:
    check_list = eval(ReadConfig().get_config(project_path.case_config_path,
                                              'CHECKLEAVEAMOUNT',
                                              'check_list'))
    Cookie = None
    loanId = None
    NoRegTel = pd.read_excel(project_path.test_case_path,
                             sheet_name="init").iloc[0, 0]
    #参照init表单查看我们这个变量的用处
    normal_tel = pd.read_excel(project_path.test_case_path,
                               sheet_name="init").iloc[1, 0]
    admin_tel = pd.read_excel(project_path.test_case_path,
                              sheet_name="init").iloc[2, 0]
    loan_member_id = pd.read_excel(project_path.test_case_path,
                                   sheet_name="init").iloc[3, 0]
示例#10
0
def get_suntime():
    location = ReadConfig('location').get()
    city = LocationInfo(name=location['city'],
                        region=location['country'],
                        timezone=location['timezone'],
                        latitude=location['latitude'],
                        longitude=location['longitude'])
    city_sun = sun(city.observer,
                   date=datetime.date.today(),
                   tzinfo=city.timezone)

    city_sunrise = city_sun['sunrise'].time()
    city_sunset = city_sun['sunset'].time()

    print(city_sunrise.minute, city_sunrise.hour, city_sunset.minute,
          city_sunset.hour)
示例#11
0
class OPMysql(object):

    __pool = None
    data = ReadConfig()

    def __init__(self):
        # 构造函数,创建数据库连接、游标
        self.conn = OPMysql.getmysqlconn()
        self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

    # 数据库连接池连接
    @staticmethod
    def getmysqlconn():
        host = OPMysql.data.get_db("mysql", "host")
        user = OPMysql.data.get_db("mysql", "user")
        password = OPMysql.data.get_db("mysql", "passwd")
        db = OPMysql.data.get_db("mysql", "db")
        port = OPMysql.data.get_db("mysql", "port")
        charset = OPMysql.data.get_db("mysql", "charset")
        if OPMysql.__pool is None:
            __pool = PooledDB(creator=pymysql,
                              mincached=1,
                              maxcached=20,
                              host=host,
                              user=user,
                              passwd=password,
                              db=db,
                              port=int(port),
                              charset=charset)
        return __pool.connection()

    # 插入\更新\删除sql
    def op_execute(self, sql, data):
        related_num = self.cur.execute(sql, data)
        self.conn.commit()
        return related_num

    # 查询sql
    def op_search(self, sql):
        self.cur.execute(sql)
        select_res = self.cur.fetchall()
        return select_res

    # 释放资源
    def dispose(self):
        self.conn.close()
        self.cur.close()
示例#12
0
    def do_mysql(query,state='all'):
        db_config=eval(ReadConfig.get_config(project_path.case_config_path,'DB','config'))
        #利用这个类读取文件
        cnn = mysql.connector.connect(**db_config)

        cur = cnn.cursor(buffered = True)
        #执行语句
        cur.execute(query)
        if state==1:
            res = cur.fetchone()#数据库单l行
        else:
            res = cur.fetchall()#多行
        # print(int(res[0]+1))


        cur.close()
        cnn.close()
        return res
示例#13
0
# coding=utf-8
import requests
from common.get_base_url import base_url
from read_config import ReadConfig

# 调用readConfig从配置文件获取配置文件中的参数
agentid = ReadConfig().get_agentid('agentid')
corpid = ReadConfig().get_corpid('corpid')
agent_secret = ReadConfig().get_agent_secret('agent_secret')
txl_secret = ReadConfig().get_txl_secret('txl_secret')
apphelper_secret = ReadConfig().get_apphelper_secret('apphelper_secret')
device_secret = ReadConfig().get_device_secret('device_secret')
log_secret = ReadConfig().get_log_secret('log_secret')

agent_payload = {
                "corpid": corpid,
                "corpsecret": agent_secret
                }

txl_payload = {
                "corpid": corpid,
                "corpsecret": txl_secret
                }

apphelper_payload = {
                "corpid": corpid,
                "corpsecret": apphelper_secret
                }

device_payload = {
                "corpid": corpid,
示例#14
0
import shutil
from os import getcwd, path

logging.basicConfig(
    filename="log.txt",
    filemode='a',
    format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
    datefmt='%H:%M:%S',
    level=logging.INFO)

if __name__ == '__main__':
    try:
        cwd = getcwd()
        if not path.isfile(cwd + 'log.txt'):
            open(cwd + 'log.txt', 'w')
        cfg = ReadConfig()
        switcher = {
            "#S": "site_name",
            "#I": "site_code",
            "#D": str(datetime.datetime.today().strftime('%y%m%d')),
            "#T": "000000",
            "#M": "xx:xx:xx:xx:xx:xx"
        }
        common = xc.ServerProxy('{}/xmlrpc/2/common'.format(cfg.url))
        common.version()
        uid = common.authenticate(cfg.db, cfg.username, cfg.password, {})
        models = xc.ServerProxy('{}/xmlrpc/2/object'.format(cfg.url))
        can_access_sensor = models.execute_kw(cfg.db, uid, cfg.password,
                                              'htc.sensor',
                                              'check_access_rights', ['read'],
                                              {'raise_exception': False})
示例#15
0
文件: nfpa.py 项目: P4ELTE/cbt
    def initialize(self):

        #read config
        self.rc = ReadConfig(self.config_file)
        if (self.rc == -1):
            #error during reading config
            return -1

        self.config = self.rc.getConfig()

        self.log = l.getLogger(self.__class__.__name__,
                               self.config['LOG_LEVEL'],
                               self.config['app_start_date'],
                               self.config['LOG_PATH'])

        self.pid_file = self.config['MAIN_ROOT'] + "/" + "nfpa.pid"
        self.log.info("Deleting previous pid_file: %s" % self.pid_file)
        os.system("rm -rf " + self.pid_file)

        #before fresh start remove temporary files if they were not removed
        #already. This could be happen, if in some case, NFPA crashes, and
        #temporary res files in PKTGEN_ROOT/ still remains existing and can
        #influence a latter measurement results in a wrong way
        self.log.info("Clean up old .res files in PKTGEN's root dir...")
        self.deleteResFiles()
        self.log.info("[DONE]")

        #create a tmp directory for flow rules under nfpa/of_rules
        path = self.config["MAIN_ROOT"] + "/of_rules/tmp"
        if not os.path.exists(path):
            os.makedirs(path)

        self.log.debug("tmp directory created under of_rules")


        self.log.info("### Measurement scenario '" + self.scenario_name + \
                      "' has been initiated ###")

        #append scenario name to self.config dictionary for later usage
        self.config['scenario_name'] = self.scenario_name

        self.log.debug(str(self.config))
        #assembling log file path
        self.log_file_path = self.config['MAIN_ROOT'] + "/log/log_" + \
                             df.getDateFormat(self.config['app_start_date']) +\
                             ".log"

        self.log.info("Log file for this measurement is: %s" %
                      self.log_file_path)
        self.log.info("THANKS FOR USING NFPA FOR MEASURING")

        self.storePID(str(os.getpid()))
        self.log.debug("NFPA PID stored")

        # create an instance of the EmailAdapter and store this object in self.config
        # if email service was enabled in the config file
        if self.config['email_service'].lower() == "true":
            self.config['email_adapter'] = EmailAdapter(self.config)
        else:
            self.config['email_adapter'] = None

        #adding no_plot variable to self.config to be able to share it later with visualizer
        self.config['no_plot'] = self.no_plot
示例#16
0
import unittest
from ddt import ddt, data
from read_config import ReadConfig
import HTMLTestRunnerNew
#导入测试报告模块
import HTMLTestRunnerNew
#导入被测试类(即需要测试的接口代码)
from unittest_math_method import MathMethod
from unittest_testcase_excel import DoExcel
from unittest_testcase_excel import GetExcel

excel_path = GetExcel("testdata.xlsx").Get_Excel_Path()
excel_title = "testdata"
button = ReadConfig().read_config("case.config", 'FLAG', 'button')
case_id_list = eval(ReadConfig().read_config("case.config", 'FLAG',
                                             'cse_id_list'))
test_data = DoExcel(excel_path, excel_title).do_excel2(button,
                                                       case_id_list)  #列表数据


#优化测试用例
@ddt
class Test_Add_ddt(unittest.TestCase):  #测试类
    def setUp(self):
        self.t = MathMethod()
        # self.doexcel = DoExcel()
        print("********************")
        print("Test Begin:")
        # print("Test describtion:{0}".format(self.test_des))

    def tearDown(self):
示例#17
0
文件: nfpa.py 项目: cslev/nfpa
    def initialize(self):

        
        #read config
        self.rc = ReadConfig(self.config_file)
        if(self.rc == -1):
            #error during reading config
            return -1
            
        self.config = self.rc.getConfig()



        self.log = l.getLogger(self.__class__.__name__,
                               self.config['LOG_LEVEL'],
                               self.config['app_start_date'],
                               self.config['LOG_PATH'])
        
        
        self.pid_file=self.config['MAIN_ROOT'] + "/" + "nfpa.pid"
        self.log.info("Deleting previous pid_file: %s" % self.pid_file)
        os.system("rm -rf " + self.pid_file)
        
        #before fresh start remove temporary files if they were not removed
        #already. This could be happen, if in some case, NFPA crashes, and
        #temporary res files in PKTGEN_ROOT/ still remains existing and can
        #influence a latter measurement results in a wrong way
        self.log.info("Clean up old .res files in PKTGEN's root dir...")
        self.deleteResFiles()
        self.log.info("[DONE]")

        #create a tmp directory for flow rules under nfpa/of_rules
        path=self.config["MAIN_ROOT"] + "/of_rules/tmp"
        if not os.path.exists(path):
            os.makedirs(path)

        self.log.debug("tmp directory created under of_rules")
        
        
        self.log.info("### Measurement scenario '" + self.scenario_name + \
                      "' has been initiated ###")
        
        
        #append scenario name to self.config dictionary for later usage
        self.config['scenario_name'] = self.scenario_name

        
        self.log.info(str(self.config))
        #assembling log file path
        self.log_file_path = self.config['MAIN_ROOT'] + "/log/log_" + \
                             df.getDateFormat(self.config['app_start_date']) +\
                             ".log"

        self.log.info("Log file for this measurement is: %s" % self.log_file_path)
        self.log.info("THANKS FOR USING NFPA FOR MEASURING")

        self.storePID(str(os.getpid()))
        self.log.debug("NFPA PID stored")

        # create an instance of the EmailAdapter and store this object in self.config
        # if email service was enabled in the config file
        if self.config['email_service'].lower() == "true":
            self.config['email_adapter'] = EmailAdapter(self.config)
        else:
            self.config['email_adapter'] = None
示例#18
0
文件: nfpa.py 项目: P4ELTE/cbt
class NFPA(object):
    '''This is the main class'''
    def __init__(self, **kwargs):
        '''
        Constructor
         - initiate config file reading and scenario name
         kwargs.scenario_name String - name of the scenario
         
        '''
        self.config = {}
        #default name TEST
        self.scenario_name = kwargs.get("scenario_name", "TEST")
        self.reset_terminal = kwargs.get("reset_terminal", True)
        self.no_database = kwargs.get("no_database", False)
        self.config_file = kwargs.get("config_file", "nfpa.cfg")

        self.no_plot = kwargs.get("no_plot", False)

    def storePID(self, new_pid):
        '''
        This process save the new_pid variables into nfpa.pids file to be able
        to kill the whole process tree during execution
        new_pid Int - the pid to store
        '''

        file = open(self.pid_file, 'w')
        file.write(str(new_pid))
        file.write("\n")
        file.close()

    def initialize(self):

        #read config
        self.rc = ReadConfig(self.config_file)
        if (self.rc == -1):
            #error during reading config
            return -1

        self.config = self.rc.getConfig()

        self.log = l.getLogger(self.__class__.__name__,
                               self.config['LOG_LEVEL'],
                               self.config['app_start_date'],
                               self.config['LOG_PATH'])

        self.pid_file = self.config['MAIN_ROOT'] + "/" + "nfpa.pid"
        self.log.info("Deleting previous pid_file: %s" % self.pid_file)
        os.system("rm -rf " + self.pid_file)

        #before fresh start remove temporary files if they were not removed
        #already. This could be happen, if in some case, NFPA crashes, and
        #temporary res files in PKTGEN_ROOT/ still remains existing and can
        #influence a latter measurement results in a wrong way
        self.log.info("Clean up old .res files in PKTGEN's root dir...")
        self.deleteResFiles()
        self.log.info("[DONE]")

        #create a tmp directory for flow rules under nfpa/of_rules
        path = self.config["MAIN_ROOT"] + "/of_rules/tmp"
        if not os.path.exists(path):
            os.makedirs(path)

        self.log.debug("tmp directory created under of_rules")


        self.log.info("### Measurement scenario '" + self.scenario_name + \
                      "' has been initiated ###")

        #append scenario name to self.config dictionary for later usage
        self.config['scenario_name'] = self.scenario_name

        self.log.debug(str(self.config))
        #assembling log file path
        self.log_file_path = self.config['MAIN_ROOT'] + "/log/log_" + \
                             df.getDateFormat(self.config['app_start_date']) +\
                             ".log"

        self.log.info("Log file for this measurement is: %s" %
                      self.log_file_path)
        self.log.info("THANKS FOR USING NFPA FOR MEASURING")

        self.storePID(str(os.getpid()))
        self.log.debug("NFPA PID stored")

        # create an instance of the EmailAdapter and store this object in self.config
        # if email service was enabled in the config file
        if self.config['email_service'].lower() == "true":
            self.config['email_adapter'] = EmailAdapter(self.config)
        else:
            self.config['email_adapter'] = None

        #adding no_plot variable to self.config to be able to share it later with visualizer
        self.config['no_plot'] = self.no_plot

    def exit(self, msg="EXITING..."):
        '''
        Print out MSG and call system.exit with ERROR status -1.
        '''
        self.log.error(msg)
        if (self.config['email_adapter'] is not None) and \
            (not self.config['email_adapter'].sendErrorMail()):
            self.log.error("Sending ERROR email did not succeed...")
        exit(-1)

    def configureVNFRemote(self, traffictype):
        '''
        Configure the remote vnf via pre-installed tools located on the same
        machine where NFPA is.  Only works for some predefined vnf_function
        and traffictraces.
        '''
        if not self.config["control_nfpa"]:
            return  # Nothing to do

        obj = self.config.get("control_obj")
        ok = False
        if not obj:
            self.exit("Plugin for control_vnf not found: %s" % obj)
        try:
            ok = obj.configure_remote_vnf(traffictype)
        except Exception as e:
            self.log.debug('%s' % e)
        if not ok:
            self.exit("Failed to configure vnf. Traffictype: %s" % traffictype)

    def stopVNFRemote(self):
        if not self.config["control_nfpa"]:
            return  # Nothing to do

        obj = self.config.get("control_obj")
        try:
            obj.stop_remote_vnf()
        except Exception as e:
            self.log.debug('%s' % e)
            self.exit("Failed to stop vnf")

    def startAnalyzing(self, traffic_type, traffic_trace):
        '''
        This function actually called after pktgen measurements are done, and it instantiate
        results_analyzer and visualizer class, to analyze the successful result files and
        create plots of the results
        :return:
        '''

        tt = traffic_type
        trace = traffic_trace

        #to indicate the email_adapter, which traffic type is analyzed
        is_synthetic = True
        if tt == "realistic":
            is_synthetic = False

        self.log.debug("Analyzing trace (%s,%s)" % (tt, trace))
        curframe = inspect.currentframe()
        calframe = inspect.getouterframes(curframe, 2)
        self.log.debug('caller name: %s' % calframe[1][3])

        # #synthetic and realistic results are process differently, so
        # #different class variables are used to store the data

        # Pktgen (re)start(s) finished, analyze results
        results_analyzer = ResultsAnalyzer(self.config,
                                           trafficType=tt,
                                           traffic_trace=trace)

        # after analyzation is done, visualize results
        results = results_analyzer.getResultsDict()

        visualizer = Visualizer(config=self.config,
                                results=results,
                                type=tt,
                                traffic_trace=trace)

        #check whether user wants to store the results in the database
        if not self.no_database:
            database_handler = DatabaseHandler(config=self.config,
                                               results=results,
                                               type=tt,
                                               traffic_trace=trace)
        # send notification email -- Last boolean parameter indicates synthetic case
        if (self.config['email_adapter'] is not None) and \
            (not self.config['email_adapter'].sendResultsMail(trace, is_synthetic)):
            self.log.warn("Sending email did not succeed...SKIPPING")

    def repeatedly_call_pktgen(self, cmd):
        self.log.info("PKTgen command: %s" % cmd)

        #sleep 1s for reading command
        time.sleep(1)

        #change dir to pktgen's main dir
        cd_cmd = "cd " + self.config["PKTGEN_ROOT"]

        #concatenate main command
        main_cmd = cd_cmd + " && " + cmd

        #start pktgen in measurement_num times
        for i in range(0, int(self.config["measurement_num"])):
            #here should be start the actual pktgen command!
            #we can't use our invoke function, since we could
            #not follow pktgen's output due to forking
            retval = os.system(main_cmd)
            if (retval != 0):
                self.exit("ERROR OCCURRED DURING STARTING PKTGEN")

    def startPktgenMeasurements(self):
        '''
        This  function is actually doing the stuff. It assembles the pktgen command
        and corresponding lua scripts, then starts the measurements
        :return:
        '''
        self.log.info("+----------------------------------------------+")
        self.log.info(
            str("|-    Estimated time required: %s        -|" %
                self.config['ETL']))
        self.log.info("+----------------------------------------------+")
        time.sleep(2)

        if self.config["trafficTypes"]:

            self.log.info(
                str("Pktgen will be started %s times" %
                    self.config["measurement_num"]))

            #iterate through traffic types
            for trafficType in self.config["trafficTypes"]:
                self.log.info("Traffic type: %s" % trafficType)
                self.configureVNFRemote(trafficType)

                #first, measure simple scenarios (if desired)
                if (trafficType == "simple"):
                    #create config file for LUA script
                    self.rc.generateLuaConfigFile(trafficType,
                                                  self.config["packetSizes"],
                                                  None)
                    #append simple lua script to pktgen command
                    cmd = self.rc.assemblePktgenCommand()
                    cmd += " -f nfpa_simple.lua"

                    self.repeatedly_call_pktgen(cmd)

                else:
                    for ps in self.config['packetSizes']:
                        #create config file for LUA script
                        self.rc.generateLuaConfigFile(trafficType, [ps], None)
                        #create the command first part
                        cmd = self.rc.assemblePktgenCommand()
                        #no special bidirectional traffic was not set
                        if not sbtc.checkSpecialTraffic(trafficType):
                            cmd += " -f nfpa_traffic.lua -s " + \
                                  self.config["sendPort"] + ":" + \
                                  self.config['MAIN_ROOT'] + \
                                  "/PCAP/nfpa." +\
                                  trafficType + "." + ps + "bytes.pcap"

                            #if bidDir is set, we need to set pcap file for the
                            #other port as well (add this part to the cmd)
                            if (int(self.config["biDir"]) == 1):
                                cmd +=  " -s " + self.config["recvPort"] +\
                                        ":" + self.config['MAIN_ROOT'] +\
                                        "/PCAP/nfpa." +\
                                        trafficType + "." + ps + "bytes.pcap"
                        else:
                            #special bidirectional traffic was set
                            tmp_tt = sbtc.splitTraffic(trafficType)
                            cmd += " -f nfpa_traffic.lua -s " + \
                                    self.config["sendPort"] + ":" + \
                                    self.config['MAIN_ROOT'] + \
                                    "/PCAP/nfpa." + tmp_tt[0] + "." + \
                                    ps + "bytes.pcap"
                            cmd +=  " -s " + self.config["recvPort"] + \
                                    ":" + self.config['MAIN_ROOT'] + \
                                    "/PCAP/nfpa." + tmp_tt[1] + "." + \
                                    ps + "bytes.pcap"

                        self.repeatedly_call_pktgen(cmd)
                    #ok, we got measurements for a given traffic trace
                    #with all the defined packetsizes

                self.stopVNFRemote()
                # Analyze results, make plots and insert into the database
                self.startAnalyzing("synthetic", trafficType)

        if self.config["realisticTraffics"]:
            #check realistic traffic traces
            for realistic in self.config["realisticTraffics"]:

                #create config file for LUA script
                self.rc.generateLuaConfigFile(None, None, realistic)
                cmd = self.rc.assemblePktgenCommand()

                #no special bidirectional traffic was not set
                if not sbtc.checkSpecialTraffic(realistic):
                    cmd +=" -f nfpa_realistic.lua -s " + \
                          self.config["sendPort"] + ":" + \
                          self.config['MAIN_ROOT'] + "/PCAP/nfpa." +\
                          realistic + ".pcap"

                    #if bidDir is set, we need to set pcap file for the
                    #other port as well (add this part to the cmd)
                    if (int(self.config["biDir"]) == 1):
                        cmd += " -s " + self.config["recvPort"] + ":" + \
                               self.config['MAIN_ROOT'] + "/PCAP/nfpa." +\
                               realistic + ".pcap"

                #special bidirectional traffic was set
                else:
                    tmp_tt = sbtc.splitTraffic(realistic)
                    cmd += " -f nfpa_realistic.lua -s " + \
                           self.config["sendPort"] + ":" + \
                           self.config['MAIN_ROOT'] + "/PCAP/nfpa." +\
                           tmp_tt[0] + ".pcap"

                    cmd +=  " -s " + self.config["recvPort"] + \
                            ":" + self.config['MAIN_ROOT'] + \
                            "/PCAP/nfpa." + tmp_tt[1] + ".pcap"

                self.repeatedly_call_pktgen(cmd)

                self.stopVNFRemote()
                self.startAnalyzing("realistic", realistic)

        #after everything is done, delete unnecessary res files
        self.deleteResFiles()

        stop = time.time()
        start = self.config['app_start_date']

        running_time = float(stop) - float(start)
        running_time = str(datetime.timedelta(seconds=running_time))
        self.log.info(str("Time elapsed: %s") % running_time)

        self.log.info("Log file can be found under: %s" % self.log_file_path)
        self.log.info("THANK YOU FOR USING NFPA %s" % self.config['version'])

        if (self.reset_terminal):
            self.log.info("Resetting terminal...")
            time.sleep(1)
            os.system("reset")
            #print out log automatically in this case to simulate 'no-reset' effect
            print_log_cmd = "cat " + self.log_file_path
            os.system(print_log_cmd)

    def deleteResFiles(self):
        '''
        This function will delete all the temporary results files under
        pktgen's main directory
        '''
        #all files look like nfpa.[traffic_type].[packetsize]bytes.res
        #besides those, only 2 symlinks exist, which could also be deleted,
        #since each restart it is recreated. However, we do not delete them!
        del_cmd = "rm -rf " + self.config["PKTGEN_ROOT"] + "/nfpa.*.res"
        invoke.invoke(command=del_cmd, logger=self.log)
示例#19
0
import xlrd
from read_config import ReadConfig

localConfig = ReadConfig()
filePath = localConfig.file_path('testfile', 'testfile.xlsx')


class ReadExcel:
    def read_excel(self, sheetname):
        cls = []
        excelFile = xlrd.open_workbook(filePath)
        sheet = excelFile.sheet_by_name(sheetname)
        nrows = sheet.nrows
        for nrow in range(nrows):
            if sheet.row_values(nrow)[0] != 'case_name':
                cls.append(sheet.row_values(nrow))
        return cls
示例#20
0
文件: nfpa.py 项目: cslev/nfpa
class NFPA(object):
    '''This is the main class'''
    
    def __init__(self, **kwargs):
        '''
        Constructor
         - initiate config file reading and scenario name
         kwargs.scenario_name String - name of the scenario
         
        '''
        self.config = {}
        #default name TEST
        self.scenario_name = kwargs.get("scenario_name","TEST")
        self.reset_terminal = kwargs.get("reset_terminal", True)
        self.no_database = kwargs.get("no_database", False)
        self.config_file = kwargs.get("config_file", "nfpa.cfg")
        
  
    def storePID(self, new_pid):
        '''
        This process save the new_pid variables into nfpa.pids file to be able
        to kill the whole process tree during execution
        new_pid Int - the pid to store
        '''
        
        file = open(self.pid_file,'w')
        file.write(str(new_pid))
        file.write("\n")
        file.close()
        
    def initialize(self):

        
        #read config
        self.rc = ReadConfig(self.config_file)
        if(self.rc == -1):
            #error during reading config
            return -1
            
        self.config = self.rc.getConfig()



        self.log = l.getLogger(self.__class__.__name__,
                               self.config['LOG_LEVEL'],
                               self.config['app_start_date'],
                               self.config['LOG_PATH'])
        
        
        self.pid_file=self.config['MAIN_ROOT'] + "/" + "nfpa.pid"
        self.log.info("Deleting previous pid_file: %s" % self.pid_file)
        os.system("rm -rf " + self.pid_file)
        
        #before fresh start remove temporary files if they were not removed
        #already. This could be happen, if in some case, NFPA crashes, and
        #temporary res files in PKTGEN_ROOT/ still remains existing and can
        #influence a latter measurement results in a wrong way
        self.log.info("Clean up old .res files in PKTGEN's root dir...")
        self.deleteResFiles()
        self.log.info("[DONE]")

        #create a tmp directory for flow rules under nfpa/of_rules
        path=self.config["MAIN_ROOT"] + "/of_rules/tmp"
        if not os.path.exists(path):
            os.makedirs(path)

        self.log.debug("tmp directory created under of_rules")
        
        
        self.log.info("### Measurement scenario '" + self.scenario_name + \
                      "' has been initiated ###")
        
        
        #append scenario name to self.config dictionary for later usage
        self.config['scenario_name'] = self.scenario_name

        
        self.log.info(str(self.config))
        #assembling log file path
        self.log_file_path = self.config['MAIN_ROOT'] + "/log/log_" + \
                             df.getDateFormat(self.config['app_start_date']) +\
                             ".log"

        self.log.info("Log file for this measurement is: %s" % self.log_file_path)
        self.log.info("THANKS FOR USING NFPA FOR MEASURING")

        self.storePID(str(os.getpid()))
        self.log.debug("NFPA PID stored")

        # create an instance of the EmailAdapter and store this object in self.config
        # if email service was enabled in the config file
        if self.config['email_service'].lower() == "true":
            self.config['email_adapter'] = EmailAdapter(self.config)
        else:
            self.config['email_adapter'] = None



    def exiting(self):
        '''
        This small function only prints out EXITING and call system.exit with
        ERROR status -1.
        Used only for function checkConfig()'s return values 
        '''
        self.log.error("EXITING...")
        if (self.config['email_adapter'] is not None) and \
            (not self.config['email_adapter'].sendErrorMail()):
            self.log.error("Sending ERROR email did not succeed...")
        exit(-1)
        
    def configureVNFRemote(self, vnf_function, traffictype):
        '''
        This function will configure the remote vnf via pre-installed tools
        located on the same machine where NFPA is.
        Only works for some predefined vnf_function and traffictraces

        :return: True - if success, False - if not
        '''

        #the path to the openflow rules
        of_path = self.config["MAIN_ROOT"] + "/of_rules/"
        # temporary variable for bidir status - it is needed for flow_rules_preparator
        bidir = False

        #handle here OpenFlow and setup via ovs-ofctl
        if self.config["control_vnf"].lower() == "openflow":

            # first, delete the flows
            ofctl_cmd = self.config["control_path"] + " " + \
                        self.config["control_args"] +\
                        " <C> " + \
                        self.config["control_mgmt"] + " "
            cmd = ofctl_cmd.replace("<C>", "del-flows")
            self.log.debug("control cmd: %s" % cmd)
            invoke.invoke(command=cmd,
                          logger=self.log,
                          email_adapter=self.config['email_adapter'])
            self.log.info("Flow rules deleted")

            # second, delete groups
            cmd = ofctl_cmd.replace("<C>", "del-groups")
            self.log.debug("control cmd: %s" % cmd)
            invoke.invoke(command=cmd,
                          logger=self.log,
                          email_adapter=self.config['email_adapter'])
            self.log.info("Groups deleted")

            #OK, flows are deleted, so replace 'del-flows' to 'add-flows' for
            # easier usage later
            cmd = ofctl_cmd.replace("<C>", "add-flows")
            #first check vnf_function, if it is bridge, then no special stuff needs
            #to be setup regardless of the traces
            ############     BRIDGE ###########
            if self.config["vnf_function"].lower() == "bridge":
                #add birdge rules - located under of_rules
                scenario_path = vnf_function + "_unidir.flows"
                if not (os.path.isfile(str(of_path + scenario_path))):
                    self.log.error("Missing flow rule file: %s" % scenario_path)
                    self.log.error("NFPA does not know how to configure VNF to act as a bridge")
                    self.log.error("More info: http://ios.tmit.bme.hu/nfpa")
                    if (self.config['email_adapter'] is not None) and \
                        (not self.config['email_adapter'].sendErrorMail()):
                        self.log.error("Sending ERROR email did not succeed...")
                    exit(-1)

                if self.config["biDir"] == 1:
                    #change flow rule file if bidir was set
                    scenario_path = scenario_path.replace("unidir","bidir")
                    bidir=True

                #prepare flow rule file
                scenario_path = flow_prep.prepareOpenFlowRules(self.log,
                                                               of_path,
                                                               scenario_path,
                                                               self.config["control_vnf_inport"],
                                                               self.config["control_vnf_outport"],
                                                               bidir)
                cmd = ofctl_cmd.replace("<C>","add-flows") + scenario_path
                self.log.info("add-flows via '%s'" % cmd)
                invoke.invoke(command=cmd,
                              logger=self.log,
                              email_adapter=self.config['email_adapter'])
                # print out stdout if any
                self.log.info("Flows added")
                return True
            ############    =============   ###########


            ############     OTHER CASES    ###########
            #check whether flow rules exists?
            #convention vnf_function.trace_direction.flows
            scenario_path = vnf_function + "." + traffictype + "_unidir.flows"
            if not (os.path.isfile(str(of_path + scenario_path))):
                self.log.error("Missing flow rule file: %s" % scenario_path)
                self.log.error("NFPA does not know how to configure VNF to act as " + \
                               "%s for the given trace %s" % (vnf_function,traffictype))
                self.log.error("More info: http://ios.tmit.bme.hu/nfpa")
                if (self.config['email_adapter'] is not None) and \
                    (not self.config['email_adapter'].sendErrorMail()):
                    self.log.error("Sending ERROR email did not succeed...")
                exit(-1)


            #If flow file exists try to find corresponding groups
            scenario_path = scenario_path.replace(".flows",".groups")
            self.log.info("Looking for group file: %s" % scenario_path)
            if (os.path.isfile(str(of_path + scenario_path))):
                self.log.info("Group file found for this scenario: %s" % scenario_path)
                #prepare group file, i.e., replace port related meta data
                group_path = flow_prep.prepareOpenFlowRules(self.log,
                                                               of_path,
                                                               scenario_path,
                                                               self.config["control_vnf_inport"],
                                                               self.config["control_vnf_outport"],
                                                               False) #TODO: bidir handling here
                cmd = ofctl_cmd.replace("<C>","add-groups")
                cmd += " " + group_path
                self.log.info("add-groups via '%s'" % cmd)
                invoke.invoke(command=cmd,
                              logger=self.log,
                              email_adapter=self.config['email_adapter'])
            else:
                self.log.info("No group file was found...continue")

            #change back to the .flows file from .groups
            scenario_path = scenario_path.replace(".groups", ".flows")

            #if biDir is set, then other file is needed where the same rules are present
            #in the reverse direction
            if (int(self.config["biDir"]) == 1):
                #biDir for remote vnf configuration is currently not supported!
                self.log.error("Configuring your VNF by NFPA for bi-directional scenario " +
                               "is currently not supported")
                self.log.error("Please verify your nfpa.cfg")
                if (self.config['email_adapter'] is not None) and \
                    (not self.config['email_adapter'].sendErrorMail()):
                    self.log.error("Sending ERROR email did not succeed...")
                exit(-1)
                #save biDir setting in a boolean to later use for flow_prep.prepareOpenFlowRules()
                # bidir = True
                # scenario_path=scenario_path.replace("unidir","bidir")
                # if not (os.path.isfile(str(of_path + scenario_path))):
                #     self.log.error("Missing flow rule file: %s" % scenario_path)
                #     self.log.error("NFPA does not know how to configure VNF to act as " + \
                #                    "%s for the given trace %s in bi-directional mode" %
                #                    (vnf_function,traffictype))
                #     self.log.error("More info: http://ios.tmit.bme.hu/nfpa")
                #     exit(-1)

            #replace metadata in flow rule files
            scenario_path = flow_prep.prepareOpenFlowRules(self.log,
                                                           of_path,
                                                           scenario_path,
                                                           self.config["control_vnf_inport"],
                                                           self.config["control_vnf_outport"],
                                                           bidir)
            #assemble command ovs-ofctl
            cmd = ofctl_cmd.replace("<C>","add-flows") + scenario_path
            self.log.info("add-flows via '%s'" % cmd)
            self.log.info("This may take some time...")
            invoke.invoke(command=cmd,
                          logger=self.log,
                          email_adapter=self.config['email_adapter'])
            self.log.info("Flows added")
            return True
        ############    =============   ###########


        else:
            self.log.error("Currently, only openflow is supported!")
            if (self.config['email_adapter'] is not None) and \
                (not self.config['email_adapter'].sendErrorMail()):
                self.log.error("Sending ERROR email did not succeed...")
            exit(-1)


    def startAnalyzing(self, traffic_type, traffic_trace):
        '''
        This function actually called after pktgen measurements are done, and it instantiate
        results_analyzer and visualizer class, to analyze the successful result files and
        create plots of the results
        :return:
        '''


        tt = traffic_type
        trace = traffic_trace

        #to indicate the email_adapter, which traffic type is analyzed
        is_synthetic = True
        if tt == "realistic":
            is_synthetic=False


        self.log.debug("Analyzing trace (%s,%s)" % (tt,trace))
        curframe = inspect.currentframe()
        calframe = inspect.getouterframes(curframe, 2)
        self.log.debug('caller name: %s' % calframe[1][3])

        # #synthetic and realistic results are process differently, so
        # #different class variables are used to store the data

        # Pktgen (re)start(s) finished, analyze results
        results_analyzer = ResultsAnalyzer(self.config,
                                            trafficType=tt,
                                            traffic_trace=trace)

        # after analyzation is done, visualize results
        results = results_analyzer.getResultsDict()
        visualizer = Visualizer(config=self.config,
                                 results=results,
                                 type=tt,
                                 traffic_trace=trace)

        #check whether user wants to store the results in the database
        if not self.no_database:
            database_handler = DatabaseHandler(config=self.config,
                                                results=results,
                                                type=tt,
                                                traffic_trace=trace)
        # send notification email -- Last boolean parameter indicates synthetic case
        if (self.config['email_adapter'] is not None) and \
            (not self.config['email_adapter'].sendResultsMail(trace, is_synthetic)):
            self.log.warn("Sending email did not succeed...SKIPPING")


    def startPktgenMeasurements(self):
        '''
        This  function is actually doing the stuff. It assembles the pktgen command
        and corresponding lua scripts, then starts the measurements
        :return:
        '''
        self.log.info("+----------------------------------------------+")
        self.log.info(str("|-    Estimated time required: %s        -|" % 
                          self.config['ETL']))
        self.log.info("+----------------------------------------------+")
        time.sleep(2)


        if self.config["trafficTypes"]:
            
            self.log.info(str("Pktgen will be started %s times" % 
                              self.config["measurement_num"]))

            #iterate through traffic types
            for trafficType in self.config["trafficTypes"]:
                #first, measure simple scenarios (if desired)
                if(trafficType == "simple"):
                    self.log.warn("SIMPLE TRACE - %s" % trafficType)

                    # configure VNF if set
                    if self.config["control_nfpa"]:
                        if not self.configureVNFRemote(self.config["vnf_function"],trafficType):
                            # configuring vnf did not succeed
                            if (self.config['email_adapter'] is not None) and \
                                (not self.config['email_adapter'].sendErrorMail()):
                                self.log.error("Sending ERROR email did not succeed...")
                            exit(-1)

                    #create config file for LUA script
                    self.rc.generateLuaConfigFile(trafficType,
                                                  self.config["packetSizes"],
                                                  None)
                    #append simple lua script to pktgen command
                    cmd = self.rc.assemblePktgenCommand()
                    cmd += " -f nfpa_simple.lua"
                    self.log.info("PKTgen command: %s" % cmd)

                    #sleep 1s for reading command
                    time.sleep(1)

                    #change dir to pktgen's main dir
                    cd_cmd = "cd " + self.config["PKTGEN_ROOT"]

                    #concatenate main command
                    main_cmd = cd_cmd + " && " + cmd
                    #here should be start the actual pktgen command!
                    #we can't use our invoke function, since we could
                    #not follow pktgen's output due to forking

                    #start pktgen in measurement_num times
                    for i in range(0, int(self.config["measurement_num"])):
                        retval = os.system(main_cmd)
                        if (retval != 0):
                            self.log.error("ERROR OCCURRED DURING STARTING PKTGEN")

                            if (self.config['email_adapter'] is not None) and \
                                    (not self.config['email_adapter'].sendErrorMail()):
                                self.log.error("Sending ERROR email did not succeed...")
                            exit(-1)


                else:
                    # configure VNF if set
                    if self.config["control_nfpa"]:
                        if not self.configureVNFRemote(self.config["vnf_function"], trafficType):
                            # configuring vnf did not succeed
                            if (self.config['email_adapter'] is not None) and \
                                (not self.config['email_adapter'].sendErrorMail()):
                                self.log.error("Sending ERROR email did not succeed...")
                            exit(-1)

                    for ps in self.config['packetSizes']:
                        #create config file for LUA script
                        self.rc.generateLuaConfigFile(trafficType,
                                                      [ps],
                                                      None)
                        #create the command first part
                        cmd = self.rc.assemblePktgenCommand()
                        #no special bidirectional traffic was not set
                        if not sbtc.checkSpecialTraffic(trafficType):
                            cmd += " -f nfpa_traffic.lua -s " + \
                                  self.config["sendPort"] + ":" + \
                                  self.config['MAIN_ROOT'] + \
                                  "/PCAP/nfpa." +\
                                  trafficType + "." + ps + "bytes.pcap"

                            #if bidDir is set, we need to set pcap file for the
                            #other port as well (add this part to the cmd)
                            if(int(self.config["biDir"]) == 1):
                                cmd +=  " -s " + self.config["recvPort"] +\
                                        ":" + self.config['MAIN_ROOT'] +\
                                        "/PCAP/nfpa." +\
                                        trafficType + "." + ps + "bytes.pcap"
                        else:
                            #special bidirectional traffic was set
                            tmp_tt = sbtc.splitTraffic(trafficType)
                            cmd += " -f nfpa_traffic.lua -s " + \
                                    self.config["sendPort"] + ":" + \
                                    self.config['MAIN_ROOT'] + \
                                    "/PCAP/nfpa." + tmp_tt[0] + "." + \
                                    ps + "bytes.pcap"
                            cmd +=  " -s " + self.config["recvPort"] + \
                                    ":" + self.config['MAIN_ROOT'] + \
                                    "/PCAP/nfpa." + tmp_tt[1] + "." + \
                                    ps + "bytes.pcap"

                        self.log.info(cmd)
                        #sleep 1s for reading command
                        time.sleep(1)


                        #change dir to pktgen's main dir
                        cd_cmd = "cd " + self.config["PKTGEN_ROOT"]
                        #concatenate main command
                        main_cmd = cd_cmd + " && " + cmd

                        # start pktgen in measurement_num times
                        for i in range(0, int(self.config["measurement_num"])):
                            #here should be start the actual pktgen command!
                            #we can't use our invoke function, since we could
                            #not follow pktgen's output due to forking
                            retval=os.system(main_cmd)
                            if(retval != 0):
                                self.log.error("ERROR OCCURRED DURING STARTING PKTGEN")

                                if (self.config['email_adapter'] is not None) and \
                                    (not self.config['email_adapter'].sendErrorMail()):
                                    self.log.error("Sending ERROR email did not succeed...")
                                exit(-1)
                    #ok, we got measurements for a given traffic trace
                    #with all the defined packetsizes

                # Start analyzing existing results, make plots and insert
                #data into the database
                self.startAnalyzing("synthetic", trafficType)

        
        if self.config["realisticTraffics"]:                
            #check realistic traffic traces
            for realistic in self.config["realisticTraffics"]:

                #create config file for LUA script
                self.rc.generateLuaConfigFile(None, 
                                              None,
                                              realistic)
                cmd = self.rc.assemblePktgenCommand()

                #no special bidirectional traffic was not set
                if not sbtc.checkSpecialTraffic(realistic):
                    cmd +=" -f nfpa_realistic.lua -s " + \
                          self.config["sendPort"] + ":" + \
                          self.config['MAIN_ROOT'] + "/PCAP/nfpa." +\
                          realistic + ".pcap" 
                
                    #if bidDir is set, we need to set pcap file for the 
                    #other port as well (add this part to the cmd)
                    if(int(self.config["biDir"]) == 1):
                        cmd += " -s " + self.config["recvPort"] + ":" + \
                               self.config['MAIN_ROOT'] + "/PCAP/nfpa." +\
                               realistic + ".pcap"
                
                #special bidirectional traffic was set
                else:
                    tmp_tt = sbtc.splitTraffic(realistic)
                    cmd += " -f nfpa_realistic.lua -s " + \
                           self.config["sendPort"] + ":" + \
                           self.config['MAIN_ROOT'] + "/PCAP/nfpa." +\
                           tmp_tt[0] + ".pcap" 
                    
                    cmd +=  " -s " + self.config["recvPort"] + \
                            ":" + self.config['MAIN_ROOT'] + \
                            "/PCAP/nfpa." + tmp_tt[1] + ".pcap"         
                    
                self.log.info(cmd)
                
                #sleep 1s for reading command
                time.sleep(1)
                
                #change dir to pktgen's main dir
                cd_cmd = "cd " + self.config["PKTGEN_ROOT"]
                #concatenate main command
                main_cmd = cd_cmd + " && " + cmd

                # start pktgen in measurement_num times
                for i in range(0, int(self.config["measurement_num"])):
                    #here should be start the actual pktgen command!
                    #we can't use our invoke function, since we could
                    #not follow pktgen's output due to forking
                    retval=os.system(main_cmd)
                    if(retval != 0):
                        self.log.error("ERROR OCCURRED DURING STARTING PKTGEN")


                        if (self.config['email_adapter'] is not None) and \
                            (not self.config['email_adapter'].sendErrorMail()):
                            self.log.error("Sending ERROR email did not succeed...")
                        exit(-1)

                # Start analyzing existing results
                self.startAnalyzing("realistic", realistic)
             

        
        #after everything is done, delete unnecessary res files
        self.deleteResFiles()

        stop = time.time()        
        start = self.config['app_start_date'] 
         
        running_time =  float(stop) - float(start)
        running_time = str(datetime.timedelta(seconds=running_time))
        self.log.info(str("Time elapsed: %s") % running_time)

        self.log.info("Log file can be found under: %s" % self.log_file_path)
        self.log.info("THANK YOU FOR USING NFPA %s" % self.config['version'])

        if(self.reset_terminal):
            self.log.info("Resetting terminal...")
            time.sleep(1)
            os.system("reset")
            #print out log automatically in this case to simulate 'no-reset' effect
            print_log_cmd="cat " + self.log_file_path
            os.system(print_log_cmd)
              
    
    def deleteResFiles(self):
        '''
        This function will delete all the temporary results files under
        pktgen's main directory
        '''
        #all files look like nfpa.[traffic_type].[packetsize]bytes.res
        #besides those, only 2 symlinks exist, which could also be deleted,
        #since each restart it is recreated. However, we do not delete them!
        del_cmd = "rm -rf " + self.config["PKTGEN_ROOT"] + "/nfpa.*.res"
        invoke.invoke(command=del_cmd,
                      logger=self.log)
示例#21
0
from read_config import ReadConfig
cf = ReadConfig()
print(cf.get_config('path'))
示例#22
0
class TwitterApp:
    """
    Class to process the twitter data.
    """
    def __init__(self):
        """
        Constructor to initialise credentials.
        """
        self.config = ReadConfig()
        self.access_token = self.config.get_access_token()
        self.access_secret = self.config.get_access_secret()
        self.consumer_key = self.config.get_consumer_key()
        self.consumer_secret = self.config.get_consumer_secret()

    def get_auth(self):
        """
        Method to generate the auth request with credentials.
        :return: Object that stores the values for auth.
        """
        return requests_oauthlib.OAuth1(self.consumer_key,
                                        self.consumer_secret,
                                        self.access_token, self.access_secret)

    def get_tweets(self):
        """
        Method that calls twitter api url.
        :return: Response for a stream of tweets.
        """
        # url for twitter
        url = 'https://stream.twitter.com/1.1/statuses/filter.json'
        # setting query params for language,location and filtering to track '#'
        query_data = [('language', 'en'), ('locations', '-130,-20,100,50'),
                      ('track', '#')]
        # setting the url with list comprehension
        query_url = url + '?' + '&'.join(
            [str(t[0]) + '=' + str(t[1]) for t in query_data])
        # sending the request to get the stream object with required url , auth
        response = requests.get(query_url, auth=self.get_auth(), stream=True)
        print(query_url, response)
        return response

    def send_tweets_to_spark(self, twitter_resp, tcp_connection):
        """
        Method to take response from twitter and extract the tweets from the whole tweets json object.
        :param twitter_resp: Response from twitter containing tweets json object.
        :param tcp_connection: Connection to send data to the spark streaming instance.
        """
        # extracting each tweet in single lines from the response
        for line in twitter_resp.iter_lines():
            try:
                # fetching each line in json format
                all_tweets = json.loads(line)
                print(all_tweets)
                d = {
                    'hashtags': all_tweets['entities']['hashtags'],
                    'id': all_tweets['id_str'],
                    'created_at': all_tweets['created_at'],
                    'place': all_tweets['place']['name']
                }

                tcp_connection.send(bytes((json.dumps(d) + "\n"), 'utf-8'))
            except Exception as exception:
                print('Error: %s' % exception)
示例#23
0
# coding=utf-8
from read_config import ReadConfig

# 调用readConfig从配置文件获取配置文件中的参数
protocol = ReadConfig().get_protocol('protocol')
domin = ReadConfig().get_domin('domin')
basepath = ReadConfig().get_basepath('basepath')
port = ReadConfig().get_port('port')

base_url = protocol + '://' + domin + ':' + port + basepath

if __name__ == "__main__":
    print(base_url)
示例#24
0
 def __init__(self):
     self.data = ReadConfig()
示例#25
0
    def get_excel(cls, file_name):
        wb = load_workbook(file_name)  # linux路径
        test_data = []  #拿到字典里面的所有数据

        mode = eval(
            ReadConfig.get_config(project_path.case_config_path, 'MODE',
                                  'mode'))
        # tel=getattr(GetCookie,'NoRegTel')#利用反射拿到数据
        normal_tel = getattr(GetCookie, "normal_tel")  #从getdata里面直接拿数据 利用反射
        for key in mode:  #遍历配置文件里面的字典
            sheet = wb[key]  #key就是表单名
            if mode[key] == 'all':
                for i in range(2, sheet.max_row + 1):
                    row_data = {}
                    row_data["case_id"] = sheet.cell(i, 1).value  #行号
                    row_data["url"] = sheet.cell(i, 2).value
                    # row_data["data"] = sheet.cell(i, 3).value    #拿到手机号之后这个地方要做替换
                    # if sheet.cell(i,3).value.find('${normal_tel}')!= -1: #找这一行的第三列看S{tel_1}的值
                    #     # row_data['data']=sheet.cell(i, 3).value.replace('${normal_tel}',str(normal_tel))  #字符串替换
                    #     row_data['data'] = DoRegs.do_regs(sheet.cell(i, 3).value)
                    if sheet.cell(i, 3).value.find('${normal_tel}') != -1:
                        row_data['data'] = sheet.cell(i, 3).value.replace(
                            '${normal_tel}', str(normal_tel + 1))
                    else:  #如果没有拿到值
                        # row_data['data'] = sheet.cell(i, 3).value
                        row_data['data'] = DoRegs.do_regs(
                            sheet.cell(i, 3).value)

                    # if sheet.cell(i, 4).value ==None:
                    #     Doexcel().write_back(project_path.test_case_path, "invest", row_data["case_id"]+1,4,str(normal_tel))
                    if sheet.cell(i, 4).value != None:
                        if sheet.cell(i, 4).value.find('${normal_tel}') != -1:
                            # row_data['check_sql'] = DoRegs.do_regs(sheet.cell(i, 4).value)
                            # row_data["check_sql"]=sheet.cell(i, 4).value.replace('${normal_tel}',str(normal_tel))
                            row_data['check_sql'] = DoRegs.do_regs(
                                sheet.cell(i, 4).value)

                    else:
                        row_data["check_sql"] = sheet.cell(i, 4).value
                    row_data["expected"] = sheet.cell(
                        i, 5).value  #添加一个期望值到测试数据里面去
                    row_data["method"] = sheet.cell(i, 6).value
                    row_data["title"] = sheet.cell(i, 7).value
                    row_data["sheet_name"] = key
                    test_data.append(row_data)
                    cls.update_tel(normal_tel + 2, file_name, "init")  #更新手机号
            else:
                for case_id in mode[key]:  #mode可以不等于 all的时候就可以等于他的列表里面的数据
                    row_data = {}  #字典
                    print(case_id)
                    row_data["case_id"] = sheet.cell(case_id + 1,
                                                     1).value  # 行号
                    row_data["url"] = sheet.cell(case_id + 1, 2).value
                    if sheet.cell(case_id + 1,
                                  3).value.find('${normal_tel}') != -1:
                        row_data['data'] = sheet.cell(case_id + 1,
                                                      3).value.replace(
                                                          '${normal_tel}',
                                                          str(normal_tel + 1))
                    else:  #如果没有拿到值
                        # row_data['data'] = sheet.cell(i, 3).value
                        row_data['data'] = DoRegs.do_regs(
                            sheet.cell(case_id + 1, 3).value)
                    # if sheet.cell(case_id+1,3).value.find('${normal_tel}')!= -1: #找这一行的第三列看S{tel_1}的值
                    #     row_data['data']=sheet.cell(case_id+1, 3).value.replace('${normal_tel}',str(normal_tel))  #字符串替换
                    #
                    # elif sheet.cell(case_id+1,3).value.find('${normal_tel1}')!=-1:
                    #     row_data['data'] = sheet.cell(case_id+1,3).value.replace('${normal_tel1}',str(normal_tel))
                    # else:  #如果没有拿到值
                    #     row_data["data"] = sheet.cell(case_id+1, 3).value
                    #这是sql语句的处理
                    if sheet.cell(case_id + 1,
                                  4).value.find('${normal_tel}') != -1:
                        print(row_data["check_sql"])
                        row_data["check_sql"] = DoRegs.do_regs(
                            sheet.cell(case_id + 1, 4).value)
                    else:
                        row_data["check_sql"] = sheet.cell(case_id + 1,
                                                           4).value
                    row_data["expected"] = sheet.cell(
                        case_id + 1, 5).value  # 添加一个期望值到测试数据里面去
                    row_data["method"] = sheet.cell(case_id + 1, 6).value
                    row_data["title"] = sheet.cell(case_id + 1, 7).value
                    row_data["sheet_name"] = key
                    test_data.append(row_data)
                    cls.update_tel(normal_tel + 2, file_name,
                                   "init")  #更新手机号  针对excel的操作

        return test_data