1

下面的代码获取输入并将输入拆分为值“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”

4

1 回答 1

0

您可以测试是否in先输入“HttpOnly”并返回“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
于 2019-03-01T14:03:15.960 回答