【问题标题】:why am i getting attribute error "AttributeError: 'Values' object has no attribute 'interface'"为什么我得到属性错误“AttributeError:'Values'对象没有属性'interface'”
【发布时间】:2020-01-08 03:20:04
【问题描述】:

Traceback(最近一次调用最后一次): 文件“hello.py”,第 12 行,在 接口=选项.接口 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 来运行这个程序。

【问题讨论】:

  • 请在您的问题中包含完整错误消息。

标签: python object attributes


【解决方案1】:

add_optiondest 参数定义了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 ")

【讨论】:

    猜你喜欢
    • 2019-09-29
    • 2021-03-29
    • 2017-11-21
    • 2020-04-08
    • 2017-05-23
    相关资源
    最近更新 更多