Python子进程挂起 [英] Python subprocess hangs

查看:41
本文介绍了Python子进程挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下子流程...

I'm executing the following subprocess...

p.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"])

...它挂了.

但是如果我执行 kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw <漏洞利用4.txt |./rtarget 然后它执行得很好.使用输入或管道操作符有什么问题吗?

But if I execute kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget then it executes fine. Is there something wrong with using the input or piping operator?

我也试过 sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)

整个代码看起来像这样更新了建议

import subprocess as sp
import pdb

for i in range(4201265,4201323):
    pdb.set_trace()
    d = hex(i)[2:]
    output = " "
    for i in range(len(d),0,-2):
        output = output + d[i-2:i] + " "

    out_buffer = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" + output + "00 00 00 00"

    text_file = open("exploit4.txt", "w")
    text_file.write("%s" % out_buffer)

 #   sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
    with open("exploit4.txt") as inhandle:
        p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
        p2 = sp.Popen("./rtarget",stdin=p.stdout,stdout=sp.PIPE)
        [output,error] = p2.communicate()

我收到一个错误是

  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 8] Exec format error

调试后发生在fire子进程调用p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)

After debugging it occurs at the fire subprocess call p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)

推荐答案

由于您使用重定向和管道,您必须启用 shell=True

Since you're using redirection and piping, you have to enable shell=True

sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"],shell=True)

但是在两个可执行文件上使用 Popen 并将 exploit4.txt 的内容作为输入会更简洁.以下示例适用于您的情况:

but it would be much cleaner to use Popen on both executables and feeding the contents of exploit4.txt as input. Example below, adapted to your case:

import subprocess

    with open("exploit4.txt") as inhandle:
        p = subprocess.Popen("./hex2raw",stdin=inhandle,stdout=subprocess.PIPE)
        p2 = subprocess.Popen("./rtarget",stdin=p.stdout,stdout=subprocess.PIPE)
        [output,error] = p2.communicate()
        print(output)
        # checking return codes is also a good idea
        rc2 = p2.wait()
        rc = p.wait()

说明:

  1. 打开输入文件,获取其句柄inhandle
  2. 打开第一个子进程,使用 inhandlestdoutstdin 重定向到输出流.获取管道句柄(p)
  3. 打开第二个子进程,将stdin与前一个进程stdoutstdout重定向到一个输出流
  4. 让第二个进程通信.它将通过消耗其输出拉"第一个:两个进程都以管道方式工作
  5. 获取返回码并打印结果
  1. open the input file, get its handle inhandle
  2. open the first subprocess, redirecting stdin with inhandle, and stdout to an output stream. Get the pipe handle (p)
  3. open the second subprocess, redirecting stdin with previous process stdout, and stdout to an output stream
  4. let the second process communicate. It will "pull" the first one by consuming its output: both processes work in a pipe fashion
  5. get return codes and print the result

注意:您会收到格式错误",因为其中一个或两个可执行文件实际上是 shell 或其他非本地可执行文件.在这种情况下,只需将 shell=True 选项添加到相关的 Popen 调用中即可.

Note: you get "format error" because one or both executables are actually shell or other non-native executables. In that case, just add the shell=True option to the relevant Popen calls.

这篇关于Python子进程挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆