我为什么会出现属性错误“ AttributeError:“值”对象没有属性“接口””

时间:2020-01-08 03:20:04

标签: python object attributes

回溯(最近通话最近): 在第12行的文件“ hello.py”中 接口= options.interface AttributeError:“值”对象没有属性“接口”

import subprocess
import optparse

parser = optparse.OptionParser()

parser.add_option("-i", "--interface", dest=" interface ", help=" Interface to change its MAC address ")

parser.add_option("-m", "--mac", dest=" mac ", help=" new mac address ")

(options, arguments) = parser.parse_args()

interface = options.interface
mac = options.mac

print("[+] Changing mac address for " + interface + " to " + mac)

subprocess.call(["ifconfig ", interface, " down"])

subprocess.call(["ifconfig ", interface, " hw", "ether", mac])

subprocess.call(["ifconfig ", interface, " up"])

注意:-我正在使用virtualBox运行该程序。

1 个答案:

答案 0 :(得分:2)

dest

add_option参数定义Values对象的成员。您使用的名称前后都有空格。这定义了其中带有空格的成员,而经典字段访问无法访问这些成员。

要调试它,请执行以下操作:

print(dir(options))

此打印:

[' interface ', ' mac ', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', … other members ...]

删除空格,您将可以访问interfacemac

parser.add_option("-i", "--interface", dest="interface", help=" Interface to change its MAC address ")
parser.add_option("-m", "--mac", dest="mac", help=" new mac address ")