Python encrypted to .so or dll

Python Encryption

How to package Python code, so easy to call someone, but at the same time play the role of encryption, this article describes how to package and dll files so files and invocation

  • First, you need to configure the environment to install Cython gcc

.so files in Linux

  • To create a file package mytest.py
import datetime
class DataCenter():
    def gettime(self):
        print(datetime.datetime.now())
    def write_data(self):
        print("hello XiaoBei!")
  • Create a file so_test.py call
from mytest import DataCenter
 
data = DataCenter()
data.gettime()
data.write_data()

Run so_test.py, certification procedures normal

  • Create a package file setup.py
from distutils.core import setup
from Cython.Build import cythonize
#[]内是要打包的文件名,也可多个文件
setup(ext_modules = cythonize(["mytest.py"]))
  • Execution python3 setup.py build_ext

Generate build file in the current directory folder and mytest.c files, .so files in the folder build file

  • The so_test.py file into the directory so, run, you can get results

The Python package for Windows file into pyd

pyd is dll

  • mytest.py and dll_test.py same as above
  • Creating setupDll.py
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension

def main():
    # 这里为文件名 可以增加多个Extension('  ', ['  '])
    extensions = [Extension('mytest', ['mytest.py'])]
    setup(ext_modules=cythonize(extensions))

if __name__ == '__main__':
    main()
  • Enter Python setupDll build_ext in the terminal pycharm obtain pyd file

  • .Pyd file calls get under way in the build folder unchanged
# !/usr/bin/env python
# -*- coding: utf-8 -*-

#如果不想改变pyd路径,则需要在dll_test.py中加入:
import sys
sys.path.append('./build/lib.win-amd64-3.7/')
from mytest import DataCenter
def main():
    data = DataCenter()
    data.gettime()
    data.write_data()

if __name__ == '__main__':
    main()   
  • Run get results

Guess you like

Origin www.cnblogs.com/wangxiaobei2019/p/12523843.html