python错误 - attributeError:'str'对象没有属性

时间:2014-02-28 00:32:10

标签: python

我在尝试运行某些python代码时收到以下错误。

traceback (most recent call last):
File "autovlan4.py", line 48, in <module>
switch.runCmds(1, ["enable", "configure terminal", "vlan " + vlan, "interface " + iface,   "switchport mode access", "switchport access vlan " + vlan, "end" ])
AttributeError: 'str' object has no attribute 'runCmds'

我的代码是:

import re
from jsonrpclib import Server

def get_vlan_iface_switch():
while True:
    switch_to_change = raw_input("Enter the name of the switch you want to change ")
    vlan = raw_input ("what VLAN ID do you want to add? (e.g. 100) ")
    iface = raw_input("what interface do you want to add the VLAN to? (e.g. eth10) ")
    switch  = Server("https://lab:lab@" + switch_to_change + "/command-api")
    print "So, we are adding VLAN %r to interface %r on %r" % (vlan, iface,    switch_to_change)

    if raw_input("Are the details above correct? (Yes/No) ")[0].lower() == "y":
         return vlan,iface,switch_to_change
    print "Please Fix Your Entries!"


# Runs commands to add gathered config to switch

vlan,iface,switch = get_vlan_iface_switch()

print ("Making the changes...")
switch.runCmds(1, ["enable", "configure terminal", "vlan " + vlan, "interface " + iface,  "switchport mode access", "switchport access vlan " + vlan, "end" ])

print ("Change completed")

我有点迷路,对蟒蛇来说很新。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

看起来get_vlan_iface_switch()应该在第13行return vlan,iface,switch。现在,它返回switch_to_change,这是一个字符串,没有.runCmds方法。