如何解决如何使用各种可选参数弹出
我有一个过程需要在python中运行并获取输出。我如何运行它,以便用户可以指定可以在进程上运行的可选参数。到目前为止,这就是我的功能。
async def analyze_target(self,target,batchSize,delay,maxdepth,maxurls,maxwait,recursive,useragent,htmlmaxcols,htmlmaxrows):
cmd = "wappalyzer"
if batchSize != "":
cmd = cmd + " --batch-size=" + batchSize
if delay != "":
cmd = cmd + " --delay=" + delay
if maxdepth != "":
cmd = cmd + " --max-depth=" + maxdepth
if maxurls != "":
cmd = cmd + " --max-urls=" + maxurls
if maxwait != "":
cmd = cmd + " --max-wait=" + maxwait
if recursive == True:
cmd = cmd + " --recursive"
if useragent != "":
cmd = cmd + " --user-agent=" + useragent
if htmlmaxcols != "":
cmd = cmd + " --html-max-cols=" + htmlmaxcols
if htmlmaxrows != "":
cmd = cmd + " --html-max-rows=" + htmlmaxrows
cmd = cmd + " " + target
p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
tmp = p.stdout.read()
self.logger.info(tmp)
p_status = p.wait()
"""
Returns log of what was wappalyzed
"""
message = f"target {target} has been wappalyzed with output {tmp}"
# This logs to the docker logs
self.logger.info(message)
return tmp
解决方法
Popen
的第一个参数args
支持单个字符串或序列。因此,您可以在list
后面附加可选参数
cmd = [cmd,*args]
或者您也可以在函数的参数中使用** kwargs(dictionary displays)(基本上将任何命名的参数视为字典),并执行以下操作:
def analyze_target(...,**kwargs):
...
for key,value,in kwargs.items():
cmd += f" --{key.replace('_','-')}={value}" # change name since dashes cannot be used as names
...
# would look like this:
analyze_target(...,additional_argument='test')
# cmd: all other arguments + --additional-argument=test
,
不构建字符串。只需构建一个包含命令名称及其参数作为单独元素的列表即可。
async def analyze_target(self,target,batchSize,delay,maxdepth,maxurls,maxwait,recursive,useragent,htmlmaxcols,htmlmaxrows):
cmd = ["wappalyzer"]
if batchSize != "":
cmd.append("--batch-size=" + batchSize)
if delay != "":
cmd.append("--delay=" + delay)
if maxdepth != "":
cmd.append("--max-depth=" + maxdepth)
if maxurls != "":
cmd.append("--max-urls=" + maxurls)
if maxwait != "":
cmd.append("--max-wait=" + maxwait)
if recursive:
cmd.append("--recursive")
if useragent != "":
cmd.append("--user-agent=" + useragent)
if htmlmaxcols != "":
cmd.append("--html-max-cols=" + htmlmaxcols)
if htmlmaxrows != "":
cmd.append("--html-max-rows=" + htmlmaxrows)
cmd.append(target)
# Drop the shell=True,so that command is executed
# directly,without shell involvement
p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。