我正在使用python,并且我应该从命令行中读取一个文件以进行进一步处理。我的输入文件有一个二进制文件,应该读取它以便进一步处理。下面是我的输入文件sub.py:
CODE = " \x55\x48\x8b\x05\xb8\x13\x00\x00"我的主文件应该是这样的:
import pyvex
import archinfo
import fileinput
import sys
filename = sys.argv[-1]
f = open(sys.argv[-1],"r")
CODE = f.read()
f.close()
print CODE
#CODE = b"\x55\x48\x8b\x05\xb8\x13\x00\x00"
# translate an AMD64 basic block (of nops) at 0x400400 into VEX
irsb = pyvex.IRSB(CODE, 0x1000, archinfo.ArchAMD64())
# pretty-print the basic block
irsb.pp()
# this is the IR Expression of the jump target of the unconditional exit at the end of the basic block
print irsb.next
# this is the type of the unconditional exit (i.e., a call, ret, syscall, etc)
print irsb.jumpkind
# you can also pretty-print it
irsb.next.pp()
# iterate through each statement and print all the statements
for stmt in irsb.statements:
stmt.pp()
# pretty-print the IR expression representing the data, and the *type* of that IR expression written by every store statement
import pyvex
for stmt in irsb.statements:
if isinstance(stmt, pyvex.IRStmt.Store):
print "Data:",
stmt.data.pp()
print ""
print "Type:",
print stmt.data.result_type
print ""
# pretty-print the condition and jump target of every conditional exit from the basic block
for stmt in irsb.statements:
if isinstance(stmt, pyvex.IRStmt.Exit):
print "Condition:",
stmt.guard.pp()
print ""
print "Target:",
stmt.dst.pp()
print ""
# these are the types of every temp in the IRSB
print irsb.tyenv.types
# here is one way to get the type of temp 0
print irsb.tyenv.types[0]问题是,当我运行"python maincode.py sub.py“时,它将代码读取为文件的内容,但它的输出与我直接将代码添加到语句irsb = pyvex.IRSB(CODE, 0x1000, archinfo.ArchAMD64())中时完全不同。有人知道问题是什么吗?我甚至使用了从inputfile导入,但它不读取文本。
发布于 2017-09-14 02:00:21
你有没有考虑过__import__方式?
你可以这样做
mod = __import__(sys.argv[-1])
print mod.CODE并且只需传递不带.py扩展名的文件名作为命令行参数:
python maincode.py sub编辑:显然,使用__import__就是discouraged。不过,您可以使用importlib模块:
import sys,importlib
mod = importlib.import_module(sys.argv[-1])
print mod.CODE..and它应该和使用__import__一样工作。
如果需要将路径传递给模块,一种方法是在每个目录中添加一个名为的空文件
__init__.py这将允许python将目录解释为模块名称空间,然后您可以以其模块形式传递路径:python maincode.py path.to.subfolder.sub
如果由于某种原因,您不能或不想将目录作为名称空间添加,并且不想在任何地方添加init.py文件,您也可以使用imp.find_module。相反,您的maincode.py将如下所示:
import sys, imp
mod = imp.find_module("sub","/path/to/subfolder/")
print mod.code不过,您必须编写代码,将命令行输入分解到模块部分"sub“和文件夹路径"/ path / to /subfolder/”中。这有意义吗?一旦准备好了,你就会像你期望的那样调用它,python maincode.py /path/to/subfolder/sub/
发布于 2017-09-14 01:48:19
https://stackoverflow.com/questions/46203461
复制相似问题