python之testcenter操作

一、设置python环境

1. 从以下路径中将StcPython.py文件拷贝出来

Linux:

/Installdir/Spirent_TestCenter_4.xx/Spirent_TestCenter_Application_Linux/API/Python

Windows:

C:Program Files (x86)Spirent CommunicationsSpirent TestCenter 4.xxSpirentTestCenter ApplicationAPIpython

2. 将StcPython.py文件放到您的工作文件夹中

3. 编辑StcPython.py文件,修改STC_PRIVATE_INSTALL_DIR变量,将路径指向STC的安装文件夹

Linux:

os.environ['STC_PRIVATE_INSTALL_DIR'] = "/opt/Spirent_TestCenter_4.xx/Spirent_TestCenter_Application_Linux/"  

Windows:

os.environ['STC_PRIVATE_INSTALL_DIR'] = "C:Program Files (x86)SpirentCommunicationsSpirent TestCenter 4.xxSpirent TestCenter Application"

4. 加载STC的python包  

from StcPython import StcPython
stc = StcPython()
print stc.get('system1','version')

运行结果:

'4.66.8916.0000'

二、testcenter代码阅读

1. StcPython.py

self.stcInt = __import__('StcIntPython27')  #根据python版本,导入StcIntPython,相对于给StcIntPython27取了一个别名,取名为stcInt,后续的connect、create、delete、disconnect均使用此别名

python2.7导入StcIntPython27.py

其他的导入StcIntPython.py

2.StcIntPython27.py

import _StcIntPython27    #这个文件的后缀为pyd文件,pyd为python扩展文件,一般用C、C++编写

有四个静态方法:

  1. _unpackArgs
  2. _packKeyVal
  3. _unpackGetResponseAndReturnKeyVal
  4. _unpackPerformResponseAndReturnKeyVal

 三、python API的添加

STC已经提供了tcl API,打算按照测试需要将其转换为python格式的API,具体分为以下几步:

1.先手动在STC GUI界面中确认需要组什么包,完成什么的操作,需要哪些参数,确认手动操作是OK的。

2.查看STC API官方手册Spirent_TestCenter_Automation_Obj_Ref.pdf,找到相应的命令接口。

3.使用tcl手动发下命令,确认找到的命令是否合适,参数输入是否正确。

4.转义为python格式的API。

比如说有如下需求,要求提供python接口的IGMPV3 API,按照如上步骤:

1.略

2.pdf文档中IGMPV3在Page1540、Page1391、Page1392

3.STC的本地安装路径中已经有tcl可执行程序,无法单独下载,个人电脑路径如下:

c:Program Files (x86)Spirent CommunicationsSpirent TestCenter 4.66Spirent TestCenter ApplicationTclin	clsh85.exe

如下是tcl中输入的代码,请参考:

#引用STC API ,SpirentTestCenter.tcl的路径
source {c:/Program Files (x86)/Spirent Communications/Spirent TestCenter 4.66/Spirent TestCenter Application/SpirentTestCenter.tcl}

#配置机框Ip 槽位 端口号
set classisAddress "10.27.130.21"
set slotPort1 "11/5"
set slotPort2 "11/6"

#创建一个项目
set ProjectA [stc::create project]

#在项目ProjextA 下创建一个发送端口 和一个接收端口
set TxPort [stc::create port -under $ProjectA]
set RxPort [stc::create port -under $ProjectA]

set portReturn [stc::config $TxPort -location "//$classisAddress/$slotPort1"]
set portReturn [stc::config $RxPort -location "//$classisAddress/$slotPort2"]

#在发送端口下创建StreamBlock(1)
set StreamBlock(1) [stc::create "StreamBlock" -under $TxPort -frameConfig "" -FrameLengthMode "FIXED" -FixedFrameLength "222" -name "StreamBlock_1"]


在StreamBlock(1)中添加EthII头
set StrEthII [stc::create ethernet:EthernetII -under $StreamBlock(1) -name eht_1 -srcMac 11:11:11:11:11:11 -dstMac 22:22:22:22:22:22]


#添加IPv4头 
set strIPv4 [stc::create ipv4:IPv4 -under $StreamBlock(1) -name Ipv4_1 -sourceAddr 10.10.10.10 -destAddr 20.20.20.20]

#####################################################################################
#添加igmpv3 join report
set strigmp_report [stc::create igmp:Igmpv3Report -under $StreamBlock(1) -name igmp_1 -type 22]
#添加igmpv3 join report全部参数list
set strigmp_report [stc::create igmp:Igmpv3Report -under $StreamBlock(1) -name igmp_1 -numGrpRecords 0 -type 22]

#igmp3头上添加Group Records
set grerecords [stc::create grpRecords -under $strigmp_report]
stc::create GroupRecord -under $grerecords -recordType CHANGE_TO_EXCLUDE_MODE
#igmp3头上添加Group Records全部参数list
stc::create GroupRecord -under $grerecords -auxDataLen 0 -mcastAddr 225.0.0.1
 -Name
 my_mygrouprecord -numSource 0 -recordType 4

#####################################################################################
#如果添加igmpv2 join报文,改写如下
stc::create igmp:Igmpv2Report -under $StreamBlock(1) -name igmp_1 -type 16 -maxRespTime 0 -groupAddress 225.0.0.1

#如果添加igmpv2 leave报文,改写如下
stc::create igmp:Igmpv2Report -under $StreamBlock(1) -name igmp_1 -type 17 -maxRespTime 0 -'groupAddress 225.0.0.1
#####################################################################################
#如果添加igmpv1 join报文,改写如下
stc::create igmp:Igmpv1 -under $StreamBlock(1) -name igmp_1 -type 2 -groupAddress 225.0.0.1 -unused 0 

#如果添加igmpv1 leave报文,改写如下
stc::create igmp:Igmpv1 -under $StreamBlock(1) -name igmp_1 -type 1 -groupAddress 225.0.0.1 -unused 0 

#保存xml文件
stc::perform SaveAsXml -config $ProjectA -filename d:/igmp.xml

Spirent_TestCenter_Automation_Obj_Ref.pdf中给出的均是参数列表,编程时如何拼接使用,请参考Spirent-TestCenter-Automation-Conf-Prog-Guide.pdf

以IGMPV3为例,PDF中作如下描述:

Igmpv3Report的父节点是StreamBlock,所以tcl代码如下:

set strigmp_report [stc::create igmp:Igmpv3Report -under $StreamBlock(1) -name igmp_1 -numGrpRecords 0 -type 22]

grpRecords是Igmpv3Report的子节点,参数为空,tcl代码如下:

set grerecords [stc::create grpRecords -under $strigmp_report]

GroupRecord的父节点是grpRecords,所以tcl代码如下:

stc::create GroupRecord -under $grerecords -auxDataLen 0 -mcastAddr 225.0.0.1 -Name my_mygrouprecord -numSource 0 -recordType 4

4.python转义语句涉及为公司写的代码,不再描述。

  

  

  

  

原文地址:https://www.cnblogs.com/hester/p/5956834.html