以下代码获取输入并将输入拆分为值“ HttpOnly”,然后在满足“ if”条件的情况下返回该值。
如果条件在split()本身失败,如何使值返回为NULL或“ 123”?
from soaptest.api import *
from com.parasoft.api import *
def getHeader(input, context):
headerNew = ""
strHeader = str(input).split("HttpOnly")
for i in strHeader:
if "com.abc.mb.SSO_GUID" in i:
Application.showMessage(i)
headerNew = i
return headerNew
编辑
输入-“ abcdefgHttpOnly”
输出-“ abcdefg”
输入-“ abcdefg”
输出-“ 123”
答案 0 :(得分:0)
您可以仅测试'HttpOnly'是否首先输入in
并返回'123'。
def getHeader(input):
if 'HttpOnly' not in str(input):
return '123'
headerNew = ""
strHeader = str(input).split("HttpOnly")
# Not using i as variable since it is usually used as an index
for header in strHeader:
if "com.abc.mb.SSO_GUID" in header:
# Application.showMessage(header)
headerNew = header
return headerNew
print(getHeader('com.abc.mb.SSO_GUIDabcdefgHttpOnly')) # com.abc.mb.SSO_GUIDabcdefg
print(getHeader('com.abc.mb.SSO_GUIDabcdefg')) # 123