简体   繁体   English

如何保护和编译python源代码到.so库?

[英]How to protect and compile python source code into a .so library?

I would like to protect my python source code , I know there is no absolute protection possible, but still there should be some means to make it difficult enough to do or quite time-consuming. 我想保护我的python源代码 ,我知道没有绝对的保护可能,但仍然应该有一些方法使其难以完成或非常耗费时间。 I want 我想要

1) to remove all documentation, comments automatically and 1)自动删除所有文档,注释和

2) to systematically change the names of variables and functions within a module (obfuscation?), so that I can keep an external interface (with meaningful names) while the internal names of variables and functions are impossible to pronounce. 2)系统地改变模块中变量和函数的名称(混淆?),这样我就可以保留一个外部接口(有意义的名字),而变量和函数的内部名称是不可能发音的。

Perhaps the best solution, which would make 1) and 2) redundant, is the following : 也许最好的解决方案,1)和2)多余, 如下

3) Is there a simple way to compile python modules to .so libraries , with a clear interface and which can be used by other python modules ? 3)是否有一种简单的方法将python模块编译为.so库 ,具有清晰的接口, 可以被其他python模块使用 It would be similar as building C and C++ extensions with distutils, except that the source code is python itself rather than C/C++. 它与使用distutils构建C和C ++扩展类似,只是源代码是python本身而不是C / C ++。 The idea is to organize all "secret code" into modules, compile them, and then import them in the rest of the python code which is not considered secret. 我们的想法是将所有“密码”组织到模块中,编译它们,然后将它们导入到其他不被认为是秘密的python代码中。

Again, I am aware that everything can be reverse-engineered, I think in pragmatic terms, most of the average developers would not be able to reverse-engineer code and even if they would be able, ethical/legal/timing reasons would make them think twice if they really want to work on this. 再一次,我知道一切都可以进行逆向工程,我认为从实用的角度来看,大多数普通开发人员都无法对代码进行逆向工程,即使他们能够,但是道德/法律/时间原因会使他们如果他们真的想在这方面努力,请三思而后行。

As mgilson mentioned in the comments, Cython is probably your best bet here. 正如mgilson在评论中提到的,Cython可能是你最好的选择。 Generally speaking, you can use it to convert your pure-python source code into compiled extension modules. 一般来说,您可以使用它将pure-python源代码转换为已编译的扩展模块。 While the primary intent of Cython is for enhanced performance, there shouldn't be any barriers for using it for source-code protection. 虽然Cython的主要目的是提高性能,但使用它来实现源代码保护不应该有任何障碍。 The extension modules it outputs aren't limited in any special ways so anything you were able to do from within Python before, you should be able to do from the Cython-generated extension modules. 它输出的扩展模块不受任何特殊方式的限制,所以你以前可以从Python中做到的任何事情,你应该能够从Cython生成的扩展模块中做到。 Cython does have a few known limitations in terms of supported features that may need to be worked around but, overall, it looks well suited to serving your purpose. Cython在支持的功能方面确实存在一些已知的限制,可能需要解决这些问题,但总的来说,它看起来非常适合为您的目的服务。

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

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