我在从 argpars 调用函数时遇到问题。这是我的脚本的简化版本,可以打印我给 -s 或 -p 的任何值
import argparse
def main():
parser = argparse.ArgumentParser(description="Do you wish to scan for live hosts or conduct a port scan?")
parser.add_argument("-s", dest='ip3octets', action='store', help='Enter the first three octets of the class C network to scan for live hosts')
parser.add_argument("-p", dest='ip', action='store',help='conduct a portscan of specified host')
args = parser.parse_args()
print args.ip3octets
print args.ip
然而,这对我来说在逻辑上是相同的,会产生错误:
import argparse
def main():
parser = argparse.ArgumentParser(description="Do you wish to scan for live hosts or conduct a port scan?")
parser.add_argument("-s", dest='ip3octets', action='store', help='Enter the first three octets of the class C network to scan for live hosts')
parser.add_argument("-p", dest='ip', action='store',help='conduct a portscan of specified host')
args = parser.parse_args()
printip3octets()
printip()
def printip3octets():
print args.ip3octets
def printip():
print args.ip
if __name__ == "__main__":main()
有谁知道我哪里出错了?
It is not相同,参见这个问题 https://stackoverflow.com/questions/146359/python-scope解释原因。
你有(至少)2个选择:
- 通过
args
作为函数的参数
- Make
args
一个全局变量。
我不确定其他人是否同意,但就我个人而言,我会将所有解析器功能移至if
声明,即主要内容如下所示:
def main(args):
printip3octets(args)
printip(args)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)