简体   繁体   English

如何保护Python源代码?

[英]How to protect Python source code?

Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? 是否可以仅分发Python脚本的字节码版本(.pyc文件)而不是原始的.py文件? My app embeds the Python interpreter and calls PyImport_Import to load a script. 我的应用程序嵌入了Python解释器并调用PyImport_Import来加载脚本。 How can I tell it to look for a .pyc file and import that? 我怎么能告诉它寻找.pyc文件并导入它?

Use the freeze tool, which is included in the Python source tree as Tools/freeze . 使用冻结工具,它包含在Python源代码树中作为Tools / freeze It converts Python byte code to C arrays; 它将Python字节代码转换为C数组; with a C compiler you can embed all your modules into a new program, which is then linked with the standard Python modules. 使用C编译器,您可以将所有模块嵌入到新程序中,然后将其与标准Python模块链接。

Note that freeze requires a C compiler. 请注意,freeze需要C编译器。

Other utilities: 其他工具:

1- PyInstaller 1- PyInstaller

2- Py2Exe 2- Py2Exe

3- Squeeze 3- 挤压

4- cx_freeze 4- cx_freeze

more info on effbot.org 关于effbot.org的更多信息

I did it by creating .py library and simple .py program that uses that library. 我是通过创建.py库和使用该库的简单.py程序来实现的。 Then I compiled library to .pyc and distributed: program as .py source and library as compiled .pyc. 然后我将库编译为.pyc并分发:程序为.py源代码和库编译.pyc。

Since you are writing your main program in C++, you can do whatever you want to protect your Python files. 由于您使用C ++编写主程序,因此可以执行任何保护Python文件的操作。 You could encrypt them for distribution, then decrypt them just in time to import them into the Python interpreter, for example. 例如,您可以加密它们以进行分发,然后及时解密它们以将它们导入Python解释器。

Since you are using PyImport_Import, you can write your own __import__ hook to import the modules not from a file but from a memory buffer, so your transformation to .pyc file can happen all in memory, with no understandable Python code on disk at all. 由于您使用的是PyImport_Import,您可以编写自己的__import__钩子来导入模块,而不是从文件中导入模块,而是从内存缓冲区导入模块,因此转换为.pyc文件可以在内存中进行,根本没有可理解的Python代码。

This should not be a problem, if you create a stand alone program using py2exe you only get the .pyc files. 这不应该是一个问题,如果你使用py2exe创建一个独立的程序,你只能得到.pyc文件。

Normally you don't need to tell python to look for .pyc files, it does so anyway. 通常你不需要告诉python查找.pyc文件,无论如何它都是这样做的。 Only if there is a newer .py source file this is used. 只有当有更新的.py源文件时才会使用它。

However, the level of protection of you source code may not be very high. 但是,源代码的保护级别可能不会很高。

In the interactive interpreter, that's automatic - if there is no .py, the .pyc will still be used: 在交互式解释器中,这是自动的 - 如果没有.py,仍然会使用.pyc:

$ echo 'print "hello"' > test.py
$ python -m compileall .
$ rm test.py
$ python -m test
hello
$

Could you just try if that works the same way with the API? 您是否可以尝试使用与API相同的方式?

Edited to add: I agree with Ber in that your code protection will be rather weak. 编辑补充:我同意Ber的观点,即您的代码保护将相当薄弱。 -O will remove docstrings, if that doesn't change the behaviour of your programme, that may make reconstructing behaviour harder, but what you'd really need some sort of bytecode obfuscation. -O将删除文档字符串,如果这不会改变程序的行为,可能会使重构行为更难,但你真正需要某种字节码混淆。

I don't know if a ready-made obfuscation tool exists for python, but this sounds viable, if you want to / can invest the time (and don't feel all too silly doing it, and can ship your own interpreter). 我不知道是否有现成的混淆工具为Python,但这个听起来可行,如果你想/可以投资的时间(不要觉得太傻做, 能运送您自己的解释)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM