python之testcenter操作
一、设置python环境
1. 从以下路径中将StcPython.py文件拷贝出来
Linux:
/Installdir/Spirent_TestCenter_4.xx/Spirent_TestCenter_Application_Linux/API/Python
Windows:
C:\Program Files (x86)\Spirent Communications\Spirent TestCenter 4.xx\SpirentTestCenter Application\API\python
2. 将StcPython.py文件放到您的工作文件夹中
3. 编辑StcPython.py文件,修改STC_PRIVATE_INSTALL_DIR变量,将路径指向STC的安装文件夹
Linux:
1 | os.environ[ 'STC_PRIVATE_INSTALL_DIR' ] = "/opt/Spirent_TestCenter_4.xx/Spirent_TestCenter_Application_Linux/" |
Windows:
1 | os.environ[ 'STC_PRIVATE_INSTALL_DIR' ] = "C:\Program Files (x86)\SpirentCommunications\Spirent TestCenter 4.xx\Spirent TestCenter Application" |
4. 加载STC的python包
1 2 3 | from StcPython import StcPython stc = StcPython() print stc.get( 'system1' , 'version' ) |
运行结果:
1 | '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++编写
有四个静态方法:
- _unpackArgs
- _packKeyVal
- _unpackGetResponseAndReturnKeyVal
- _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可执行程序,无法单独下载,个人电脑路径如下:
1 | c:\Program Files (x86)\Spirent Communications\Spirent TestCenter 4.66 \Spirent TestCenter Application\Tcl\ bin \tclsh85.exe |
如下是tcl中输入的代码,请参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #引用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代码如下:
1 | set strigmp_report [stc::create igmp:Igmpv3Report - under $StreamBlock( 1 ) - name igmp_1 - numGrpRecords 0 - type 22 ] |
grpRecords是Igmpv3Report的子节点,参数为空,tcl代码如下:
1 | set grerecords [stc::create grpRecords - under $strigmp_report] |
GroupRecord的父节点是grpRecords,所以tcl代码如下:
1 | stc::create GroupRecord - under $grerecords - auxDataLen 0 - mcastAddr 225.0 . 0.1 - Name my_mygrouprecord - numSource 0 - recordType 4 |
4.python转义语句涉及为公司写的代码,不再描述。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架