【问题标题】:Having trouble piping two commands using subprocess module with python3使用带有 python3 的子进程模块管道两个命令时遇到问题
【发布时间】:2021-02-23 01:28:59
【问题描述】:

这就是我得到的。 (我知道这很糟糕)它需要一个参数,即域名。在其上运行 subfinder,然后将结果写入结果。

def getSubs(command):
    result = subprocess.check_output(command, shell=True, universal_newlines=True)
    with open('results', 'w') as file_contents:
        for i in result:
            file_contents.write(i)
    http_check()

getSubs(['subfinder -d' 'sys.argv[1]'])

现在我只想看看我是否可以打开文件以将每一行输入一个新命令,并担心稍后将其写入另一个文件。我注释掉了所有内容,除了:

def httpCheck():
    with open('results', 'r') as file_to_iterate:
        for scan in file_to_iterate:
            p1 = subprocess.Popen([f"echo", "{scan}"], stdout=subprocess.PIPE)
            p2 = subprocess.Popen(['httprobe'], stdin=p1.stdout, shell=True, stdout=subprocess.PIPE)
            p1.stdout.close()
            output = p2.communicate()[0]
            print(output)

返回:

b''
b''
b''

我怎样才能让它打印出结果?或者参考。此外,new_file 现在只包含三行:

 domain.com
 another_domain.com
 non-existent.com

【问题讨论】:

    标签: python-3.x subprocess


    【解决方案1】:

    [已解决]我想我会继续回答我自己的问题。经过数小时的反复试验,终于弄明白了。这是我的解决方案。

    def httpCheck():
        with open('new_file', 'rb') as file_to_iterate:
            for scan in file_to_iterate:
                with open('results.txt', 'a') as f:
                    p1 = subprocess.Popen(["echo", scan], stdout=subprocess.PIPE)
                    p2 = subprocess.Popen(['httprobe', '-t', '1000'], stdin=p1.stdout, stdout=f)
                    output = p2.communicate()[0]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2020-12-07
      相关资源
      最近更新 更多