[英]Python subprocess hangs
I'm executing the following subprocess... 我正在执行以下子流程...
p.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"])
...and it hangs. ...它挂了。
But if I execute kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget
但是,如果我执行kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget
kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget
then it executes fine. kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget
然后执行正常。 Is there something wrong with using the input or piping operator? 使用输入或管道操作器有什么问题吗?
I also tried sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
我也尝试了sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
The entire code looks like this UPDATED WITH SUGGESTIONS 整个代码看起来像这样UPDATED SUGGESTIONS
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()
I'm getting an error is 我遇到一个错误是
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
After debugging it occurs at the fire subprocess call p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
调试后,它发生在fire子p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
调用p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
Since you're using redirection and piping, you have to enable shell=True
由于您正在使用重定向和管道,因此必须启用shell=True
sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"],shell=True)
but it would be much cleaner to use Popen
on both executables and feeding the contents of exploit4.txt
as input. 但它是更清洁的使用Popen
两个可执行文件和喂养的内容exploit4.txt
作为输入。 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()
Explanation: 说明:
inhandle
打开输入文件,得到它的手柄inhandle
stdin
with inhandle
, and stdout
to an output stream. 打开第一inhandle
,使用inhandle
将stdin
重定向,并将stdout
重定向到输出流。 Get the pipe handle (p) 获取管柄(p) stdin
with previous process stdout
, and stdout
to an output stream 打开第二个子进程,将stdin
与先前的进程stdout
重定向,并将stdout
重定向到输出流 communicate
. 让第二个进程进行communicate
。 It will "pull" the first one by consuming its output: both processes work in a pipe fashion 它将通过消耗其输出来“拉”第一个:两个进程都以管道方式工作 Note: you get "format error" because one or both executables are actually shell or other non-native executables. 注意:因为一个或两个可执行文件实际上是Shell或其他非本地可执行文件,所以会出现“格式错误”。 In that case, just add the shell=True
option to the relevant Popen
calls. 在这种情况下,只需将shell=True
选项添加到相关的Popen
调用中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.