Python 编译与反编译实例

发布时间: 更新时间: 总字数:1657 阅读时间:4m 作者:IP:上海 网址

Python运行时,会将 py 文件编译为pyc、pyo文件。本文介绍 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 的编译其实很简单,用

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

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

python
import py_compile
py_compile.compile('py_file_path')

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

bash
python -O -m py_compile py_file_name.py

说明:

  • 其中的 -m 相当于脚本中的 import,这里的-m py_compile 相当于上面的 import py_compile
  • -O 开启基础优化(移除 assert 语句及 debug 条件代码);如果改成 -OO 则是二级优化,在 -O 基础上进一步移除文档字符串(docstring)

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

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

compile 函数原型:

text
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 。使用方法如下:

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

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

bash
python -m compileall /root/src/

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

text
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 安装

bash
pip install uncompyle2

源码安装

bash
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 源码

python
def main():
    print "Hello World!"

if __name__ == '__main__':
    main()

编译 py -> pyc

bash
[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

bash
[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]#