Python subprocess hangs
<p>I'm executing the following subprocess...</p>

<p><code>p.call(["./hex2raw", "&lt;", "exploit4.txt", "|", "./rtarget"])</code></p>

<p>...and it hangs.</p>

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

<p>I also tried <code>sp.call(["./hex2raw", "&lt;", "exploit4.txt", "|", "./rtarget"], shell=True)</code></p>

<p>The entire code looks like this <strong>UPDATED WITH SUGGESTIONS</strong></p>

<pre><code>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", "&lt;", "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()
</code></pre>

<p>I'm getting an error is </p>

<pre><code>  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
</code></pre>

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

<p>Since you're using redirection and piping, you have to enable <code>shell=True</code></p>

<pre><code>sp.call(["./hex2raw", "&lt;", "exploit4.txt", "|", "./rtarget"],shell=True)
</code></pre>

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

<pre><code>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()
</code></pre>

<p>Explanation:</p>

<ol>
<li>open the input file, get its handle <code>inhandle</code></li>
<li>open the first subprocess, redirecting <code>stdin</code> with <code>inhandle</code>, and <code>stdout</code> to an output stream. Get the pipe handle (p)</li>
<li>open the second subprocess, redirecting <code>stdin</code> with previous process <code>stdout</code>, and <code>stdout</code> to an output stream</li>
<li>let the second process <code>communicate</code>. It will "pull" the first one by consuming its output: both processes work in a pipe fashion</li>
<li>get return codes and print the result</li>
</ol>

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

python subprocess