使用subprocess.call参数的Intrepret字节码

时间:2017-03-27 07:06:52

标签: python bash

我有一个程序可以采用一个参数。

我需要在我的python脚本中调用这个程序,我需要以字节码格式传递参数(比如\ x42 \ x43)。

直接在bash中,我可以这样做,它确实有效:

./myprogram $'\x42\x43'

但是使用subprocess.call它不起作用:

subprocess.call(["myprogram", "$'\x42\x43'"])

字节不是代表。

我尝试用/bin/bash调用我的程序,但我的程序返回段错误!

2 个答案:

答案 0 :(得分:0)

需要注意的一点:$是一个bash构造。它是评估变量并返回其值的那个。从另一个程序调用一个程序时,通常不会发生这种情况。因此,当您调用myprogram时,您需要以myprogram理解它们的形式提供所有参数。

答案 1 :(得分:0)

谢谢@ acw1668

这很有效: subprocess.call(["myprogram", b"\x42\x43"])

但如果您的字节码在变量中,则需要进行此操作:

# If you're string is unicode type
mystring = mystring.decode('unicode_escape')
# I choose latin1, because some of my bytecode are bigger than 128
mystring = mystring.encode('latin1')

我的字节码被正确解释: subprocess.call(["myprogram", mystring])