Return 不匹配时为 NULL

Return NULL on mismatch

下面的代码获取一个输入并在值 "HttpOnly" 处拆分输入,然后如果满足 "if" 条件,那么它 return 就是这样的值。

如果条件在 split() 本身失败,如何将 return 的值设置为 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”

您可以只测试 'HttpOnly' 是否先输入 in 和 return '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