python - 如何使用各种可选参数打开
问题描述
我有一个需要在 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
解决方案
不要构建字符串。只需构建一个包含命令名称及其参数作为单独元素的列表。
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)
...
推荐阅读
- javascript - Javascript:通过 li 中 span 的内容对列表进行排序
- angularjs - 为什么 $scope 对象无法在 view2.html 中显示来自我的控制器的数据
- sql-server - 有没有办法从案例内的存储过程中返回一个值?
- r - 是否有一个 plotly 函数可以根据数据框中的不同条件切换图的图例和颜色?
- mule - Munit 4:有没有办法在套件的不同测试用例中模拟来自相同缓存范围的 HTTP 请求的有效负载?
- c# - 如何在创建新帐户时修复“ EnumDropDownListFor ”
- java - 计数器只添加一次
- r - R中的矢量化滚动/累积马氏距离
- php - 连接表,基于 DATE 的总订单价格并获得 2 个不同日期记录的单独总和,而不是 1 个不是迄今为止唯一的
- java - 根据搜索模式列出亚马逊 S3 文件夹下所有带有文件名的 xml 文件