netstat   ano |findstr 8080  ,python subprocess 管道使用:

python  子进程管道传递

 

 

import subprocess
import threading
def demo(iter_list, if_child=True):
    if not if_child:
        sub = subprocess.Popen(iter_list, shell=True, stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
        stdout, stderr = sub.communicate()
        if sub.returncode == 0:
            print("正常输出为:", stdout)
            print("code 为:", sub.returncode)
        else:
            print("stderr is :", stderr)
    else:
        p1 = subprocess.Popen(iter_list[0], shell=False,
                              stdout=subprocess.PIPE, universal_newlines=True, stderr=subprocess.PIPE)

        p2 = subprocess.Popen(iter_list[1], shell=False, stdin=p1.stdout,
                              stdout=subprocess.PIPE, universal_newlines=True)
        stdout, stderr = p2.communicate()
        if p2.returncode == 0:
            print("正常输出为:", stdout)
            print("code 为:", p2.returncode)
        else:
            print("stderr is :", stderr)
if __name__ == '__main__':
    arg_list = [[["netstat", "-ano"], ["findstr", "8080"]] for i in range(2)]
    # a=["netstat -ano|findstr 8080" for m in range(4)]
    pool = [threading.Thread(target=demo, args=(i,)) for i in arg_list]
    for th in pool:
        th.start()
    for t in pool:
        t.join()

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2021-11-19
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
猜你喜欢
  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2021-05-14
  • 2021-06-24
  • 2021-09-05
  • 2022-01-19
相关资源
相似解决方案