Cython protection python code

Note: .pyc also have some protection, easy to decompile the source code ...

 

When publishing project, in order to prevent disclosure of the source code, the source code needs to be a protective mechanism, as used herein, Cython the .py file into .so protection. This approach, though still can decompile, but the difficulty will be relatively large. In addition, Cython is a superset of Python.

Self-installation Cython

 

1. Create a file complie.py

from Cython.Build import cythonize
from Cython.Distutils import build_ext
from setuptools import setup
from setuptools.extension import Extension

setup(
ext_modules=cythonize(
[
Extension('project.*', ['project/*.py']),
Extension('project.api.*', ['project/api/*.py']),
Extension('project.api.bizs.*', ['project/api/bizs/*.py']),
Extension('project.api.data.export*', ['project/api/data/export/*.py']),
Extension('project.api.exceptions.*', ['project/api/exceptions/*.py']),
# 需要保护的.py文件目录写在此处
],
build_dir='build',
compiler_directives=dict(
always_allow_keywords=True, language_level=3
)
),
cmdclass=dict(
build_ext=build_ext
)
)

language_level representatives python3 version, python2 wrote 2

 

2, run the command: python compile.py build_ext --inplace

 .Py will be generated for each file in each directory .so files, and a build folder. .py file has been protected, you can call each other a .so file.

Deleted deployment project .py files, build folders.

 

If the project uses celery, celery careful not to compile the code, or celery can not be used.

Posted a shell script, with the upper section of code uses.

! # / bin / bash 

# Clear the cache directory 
the Find The -type d -name __pycache__ |. xargs  RM - the RF 

# compiled code 
python3 -m Venv env 
SH  env / bin / of an activate 
python3 compile.py build_ext - InPlace
 IF [$ -? NE 0 ]; the then 
    Exit . 1 
Fi 


# change file celery 
Music Videos ./project/api/tasks/cele/__init__.py ./project/api/tasks/cele/ the __init __ py.bak.
 Music Videos ./project/api/tasks/ CELE / base.py ./project/api/tasks/cele/ base.py.bak
 mv./project/api/tasks/cele/ ./project/api/tasks/cele/export.py export.py.bak
 mv ./project/api/tasks/__init__.py ./project/api/tasks/ __init__. py.bak
 mv ./project/api/tasks/dispatch_subdomain.py ./project/api/tasks/ dispatch_subdomain.py.bak
 mv ./project/api/tasks/recognize_area.py ./project/api/tasks/ recognize_area. py.bak 


# a .so file rename 
Find ./project -name ' * .so ' | awk -F ' .cpython the x86_64-36M-GNU-Linux- '  ' {Print "Music Videos" $ 0 "" $ 2. 1 $} ' | SH 

# delete .py file 
find ./project  -name '*.py' | xargs rm -f

mv ./project/api/tasks/cele/__init__.py.bak ./project/api/tasks/cele/__init__.py
mv ./project/api/tasks/cele/base.py.bak ./project/api/tasks/cele/base.py
mv ./project/api/tasks/cele/export.py.bak ./project/api/tasks/cele/export.py
mv ./project/api/tasks/__init__.py.bak ./project/api/tasks/__init__.py
mv ./project/api/tasks/dispatch_subdomain.py.bak ./project/api/tasks/dispatch_subdomain.py
mv ./project/api/tasks/recognize_area.py.bak ./project/api/tasks/recognize_area.py 

# remove unnecessary files 
RM - the RF Build
 RM - f .gitignore
 RM - f compile.py
 RM . -f Build SH

 

END!

Guess you like

Origin www.cnblogs.com/ldy-miss/p/11649318.html