Python 编译与反编译实例

发布时间: 更新时间: 总字数:1747 阅读时间:4m 作者: 分享 复制网址

Python运行时,会将py文件编译为pycpyo文件。本文介绍 Python 编译与反编译。

基本概念

  • pyc文件:pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code

    • py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,是由python的虚拟机来执行的,这个是类似于JAVA或者.NET的虚拟机的概念
    • pyc的内容,是跟python的版本相关的,不同版本编译后的pyc文件是不同的,2.5编译的pyc文件,2.4版本的 python是无法执行的
  • pyo文件:pyo是优化编译后的程序 python -O 源文件即可将源程序编译为pyo文件 - pyd文件:pyd是python的动态链接库

  • codon 一个使用LLVM的高性能、零开销、可扩展的Python编译器

为什么需要pyc文件

  • 防止源码泄漏,可以通过修改 opcode 定制

py_compile 编译示例

生成单个pyc文件

python提供了内置的类库来实现把py文件编译为pyc文件,这个模块就是 py_compile 模块。python的编译其实很简单,用

python -m py_compile py_file_name.py
python -m py_compile /root/src/{file1,file2}.py

即可编译成pyc文件。也可以用如下脚本编译层pyc文件:

import py_compile
py_compile.compile('py_file_path')

采用用如下命令编译成pyo文件:

python -O -m py_compile py_file_name.py

说明:

  • 其中的 -m 相当于脚本中的import,这里的-m py_compile 相当于上面的 import py_compile
  • -O 如果改成 -OO 则是删除相应的 pyo文件,具体帮助可以在控制台输入 python -h 查看

使用方法非常简单,如下所示,直接在idle中,就可以把一个py文件编译为pyc文件了。(假设在windows环境下)

import py_compile
py_compile.compile(r'H:\game\test.py')

compile函数原型:

compile(file[, cfile[, dfile[, doraise]]])
  • file 表示需要编译的py文件的路径
  • cfile 表示编译后的pyc文件名称和路径,默认为直接在file文件名后加c 或者 o,o表示优化的字节码
  • dfile 这个参数英文看不明白,请各位大大赐教。(鄙视下自己)原文:it is used as the name of the source file in error messages instead of file
  • doraise 可以是两个值,True或者False,如果为True,则会引发一个PyCompileError,否则如果编译文件出错,则会有一个错误,默认显示在sys.stderr中,而不会引发异常

(来自python2.5文档)

批量生成pyc文件

一般来说,我们的工程都是在一个目录下的,一般不会说仅仅编译一个py文件而已,而是需要把整个文件夹下的py文件都编译为pyc文件,python又为了我们提供了另一个模块:compileall 。使用方法如下:

import compileall
compileall.compile_dir(r'H:\game')

也可以直接用命令行编译一个目录下的文件,如:

python -m compileall /root/src/

这样就把game目录,以及其子目录下的py文件编译为pyc文件了。嘿嘿,够方便吧。来看下compile_dir函数的说明:

compile_dir(dir[, maxlevels[, ddir[, force[, rx[, quiet]]]]])
  • dir 表示需要编译的文件夹位置
  • maxlevels 表示需要递归编译的子目录的层数,默认是10层,即默认会把10层子目录中的py文件编译为pyc
  • ddir pyc文件的运行目录。
  • force 如果为True,则会强制编译为pyc,即使现在的pyc文件是最新的,还会强制编译一次,pyc文件中包含有时间戳,python编译器会根据时间来决定,是否需要重新生成一次pyc文件
  • rx 表示一个正则表达式,比如可以排除掉不想要的目录,或者只有符合条件的目录才进行编译
  • quiet 如果为True,则编译后,不会在标准输出中,打印出信息

uncompyle2 编译示例

pip 安装

pip install uncompyle2

源码安装

git clone https://github.com/wibiti/uncompyle2.git
cd uncompyle2/
python setup.py install
# 使用
python -u uncompyle2 test_compile.pyc > test_compile.py

示例源码

test_compile.py 源码

def main():
    print "Hello World!"

if __name__ == '__main__':
    main()

编译 py -> pyc

[root@xiexianbin_cn python]# python -m py_compile test_compile.py
[root@xiexianbin_cn python]# ls
test_compile.py  test_compile.pyc
[root@xiexianbin_cn python]# hexdump -C test_compile.pyc
00000000  03 f3 0d 0a 94 cf 29 58  63 00 00 00 00 00 00 00  |......)Xc.......|
00000010  00 02 00 00 00 40 00 00  00 73 23 00 00 00 64 00  |.....@...s#...d.|
00000020  00 84 00 00 5a 00 00 65  01 00 64 01 00 6b 02 00  |....Z..e..d..k..|
00000030  72 1f 00 65 00 00 83 00  00 01 6e 00 00 64 02 00  |r..e......n..d..|
00000040  53 28 03 00 00 00 63 00  00 00 00 00 00 00 00 01  |S(....c.........|
00000050  00 00 00 43 00 00 00 73  09 00 00 00 64 01 00 47  |...C...s....d..G|
00000060  48 64 00 00 53 28 02 00  00 00 4e 73 0c 00 00 00  |Hd..S(....Ns....|
00000070  48 65 6c 6c 6f 20 57 6f  72 6c 64 21 28 00 00 00  |Hello World!(...|
00000080  00 28 00 00 00 00 28 00  00 00 00 28 00 00 00 00  |.(....(....(....|
00000090  73 0f 00 00 00 74 65 73  74 5f 63 6f 6d 70 69 6c  |s....test_compil|
000000a0  65 2e 70 79 74 04 00 00  00 6d 61 69 6e 01 00 00  |e.pyt....main...|
000000b0  00 73 02 00 00 00 00 01  74 08 00 00 00 5f 5f 6d  |.s......t....__m|
000000c0  61 69 6e 5f 5f 4e 28 02  00 00 00 52 00 00 00 00  |ain__N(....R....|
000000d0  74 08 00 00 00 5f 5f 6e  61 6d 65 5f 5f 28 00 00  |t....__name__(..|
000000e0  00 00 28 00 00 00 00 28  00 00 00 00 73 0f 00 00  |..(....(....s...|
000000f0  00 74 65 73 74 5f 63 6f  6d 70 69 6c 65 2e 70 79  |.test_compile.py|
00000100  74 08 00 00 00 3c 6d 6f  64 75 6c 65 3e 01 00 00  |t....<module>...|
00000110  00 73 04 00 00 00 09 03  0c 01                    |.s........|
0000011a
[root@xiexianbin_cn python]#

反编译 pyc -> py

[root@xiexianbin_cn python]# uncompyle6 test_compile.pyc > s.py
[root@xiexianbin_cn python]# cat s.py
# uncompyle6 version 2.9.6
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.5 (default, Sep 15 2016, 22:37:39)
# [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
# Embedded file name: test_compile.py
# Compiled at: 2016-11-14 22:52:04


def main():
		print 'Hello World!'


if __name__ == '__main__':
		main()
# okay decompiling test_compile.pyc
[root@xiexianbin_cn python]#
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数