1

我正在使用下面的代码并获得如下输出。请建议。

def isServerRunning(nodename,servername):
    """Returns a boolean to say if the server is running.
    Not 100% accurate but should be close - relies on there only being an mbean for
    a server if it's running"""

    mbean = AdminControl.queryNames('type=Server,node=%s,name=%s,*' % (nodename,servername))
    if mbean:
        return True
    return False

def chunks(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]


l = ['acsayul33450Node00', 'dmgr', 'acsayul33450Node01', 'gndtaskprd1f1', 'acsayul33450Node01', 'nodeagent', 'acsayul33451Node01', 'gndtaskprd1f2', 'acsayul33451Node01', 'nodeagent']
jvmList1 = chunks(l, 2)
sNameList = []
jvmList2 = []
N = 0
S = 0
for jvms in jvmList1:
    N += 1
    S = str(N)
    jvm = (jvms[1])
    node = (jvms[0])
    status = "stopped"
    jvmList2 = jvm
    if isServerRunning(node,jvm):
        status = "running"
        print("\n")
    sNameList.append(" %2s. %s %s %s" % (S,  jvm, node, status))
X = 1
while X:
        print("\n".join(sNameList))
        choice = raw_input("\nEnter your choice [1-%s] : " % (S))

        try:
            ServerName = jvmList2[choice]   
            X = 0
        except KeyError:
                print("\nInvalid entry\n")
print ServerName 

输出如下。

wsadmin>execfile('C:\IBM\pyscripts\test\10.py')


  1. dmgr acsayul33450Node00 running
  2. gndtaskprd1f1 acsayul33450Node01 stopped
  3. nodeagent acsayul33450Node01 stopped
  4. gndtaskprd1f2 acsayul33451Node01 stopped
  5. nodeagent acsayul33451Node01 stopped

Enter your choice [1-5] : 1
WASX7015E: Exception running command: "execfile('C:/IBM/pyscripts/test/10.py')";
exception information:
 com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
  File "C:/IBM/pyscripts/test/10.py", line 38, in ?
TypeError: sequence subscript must be integer or slice
4

1 回答 1

1
ServerName = jvmList2[choice] 

choice的是一个字符串。您应该将其转换为int第一个。

choice = int(raw_input("\nEnter your choice [1-%s] : " % (S)))
于 2013-07-24T13:57:52.987 回答