2014-02-28 106 views
0

當我嘗試運行一些python代碼時,出現了下面的錯誤。python錯誤 - attributeError:'str'對象沒有任何屬性

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:[email protected]" + 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") 

我有點失落,很新的蟒蛇。任何幫助將不勝感激。

回答

2

它看起來像get_vlan_iface_switch()應該在第13行return vlan,iface,switch。現在,它返回switch_to_change,這是一個字符串,它沒有.runCmds方法。

相關問題