在不匹配上返回null



以下代码获取输入并在值" 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

编辑

输入 - " abcdefghtponly"

输出 - " abcdefg"

输入 - " abcdefg"

输出 - " 123"

您只需测试'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

最新更新