首页 > 编程知识 正文

license授权在哪里打开,python编程例子

时间:2023-05-06 10:41:34 阅读:8909 作者:4546

python许可证机制@[TOC](python许可证机制)1.生成许可证文件2 .分析许可证文件创建python脚本3 .将分析脚本转换为. so文件4

#-* -编码: utf-8-* - importreimportsysimportdatetimeimportsubprocessfromcrypto.cipherimportaesfrombinasciiiimporta 2b _ hexfrombinasciiiiortb2a mac,date ) : self.MAC=MAC self.date=datedefencrypt ) self,content (: # contentlengthmustbeamultipleof 16.wwited encrypt content.AES=AES.new (b ' 20210520210520 ',AES.MODE_CBC, b ' 2021052020210520 ' (encrypted _ content=AES.encrypt (content ) return ) b2a _ hex (encrypted _ content ) ) ) DD ile,' w ' ) aslf : lf.write (MAC : % s (n ' % ) (MAC3360 ) ) n ) ) 65 n'%(self.date ) sign=self . self.date ) ) print ) sign3360 ) str ) sign n ' ) lf.write(sign: ) str (sign.decode ) utf-8 ) ) () ) self ) : license _ DIC=self.parse _ license _ fille sign _ list=sign.split (' # ' ) MAC=sign _ list [0] .=license_DIC['MAC'](or(date!=license _ DIC [ ' date ' ] (:打印(* error * : licensefileismodified!) ) sys.exit(1) # checkmacandeffectivedateinvalidornot.if len ) sign_list )==2: mac=self.get_mac ) cure ' (sys.exit(1) currenttimemustbebeforeeffectivedate.if sign _ list [1] current _ date 3360打印(* error * 3360 )

'r') as LF: for line in LF.readlines(): if re.match('^s*(S+)s*:s*(S+)s*$', line): my_match = re.match('^s*(S+)s*:s*(S+)s*$', line) license_dic[my_match.group(1)] = my_match.group(2) return (license_dic) def decrypt(self, content): aes = AES.new(b'2021052020210520', AES.MODE_CBC, b'2021052020210520') decrypted_content = aes.decrypt(a2b_hex(content.encode('utf-8'))) return (decrypted_content.decode('utf-8')) def get_mac(self): mac = '' SP = subprocess.含糊的绿草sbin/ifconfig', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = SP.communicate() for line in str(stdout, 'utf-8').split('n'): if re.match('^s*ethers+(S+)s+.*$', line): my_match = re.match('^s*ethers+(S+)s+.*$', line) mac = my_match.group(1) break return mac# class python2so():# py_files = ['check_license_file.py', ]# setup(ext_modules=cythonize(py_files), )if __name__ == '__main__': # make license file mac = '02:42:8f:92:4a:9d' date = '20211125' get_license_file(mac, date).gen_license_file() # check license file # check_license_file().license_check() #.py文件生成.so文件 'python check_license_file.py build_ext --inplace' 2.编写解析license文件的python脚本

check_license_file.py

#!/usr/bin/env python3import reimport sysimport datetimeimport subprocessfrom Crypto.Cipher import AESfrom binascii import a2b_hexclass check_license_file(): def license_check(self): license_dic = self.parse_license_file() sign = self.decrypt(license_dic['Sign']) sign_list = sign.split('#') mac = sign_list[0].strip() date = sign_list[1].strip() if (mac != license_dic['MAC']) or (date != license_dic['Date']): print('*Error*: License file is modified!') sys.exit(1) # Check MAC and effective date invalid or not. if len(sign_list) == 2: mac = self.get_mac() current_date = datetime.datetime.now().strftime('%Y%m%d') if sign_list[0] != mac: print('*Error*: Invalid host!') sys.exit(1) # Current time must be before effective date. if sign_list[1] < current_date: print('*Error*: License is expired!') sys.exit(1) else: print('*Error*: Wrong Sign setting on license file.') sys.exit(1) def parse_license_file(self): license_dic = {} license_file = 'License.dat' with open(license_file, 'r') as LF: for line in LF.readlines(): if re.match('^s*(S+)s*:s*(S+)s*$', line): my_match = re.match('^s*(S+)s*:s*(S+)s*$', line) license_dic[my_match.group(1)] = my_match.group(2) return (license_dic) def decrypt(self, content): aes = AES.new(b'2021052020210520', AES.MODE_CBC, b'2021052020210520') decrypted_content = aes.decrypt(a2b_hex(content.encode('utf-8'))) return (decrypted_content.decode('utf-8')) def get_mac(self): mac = '' SP = subprocess.含糊的绿草sbin/ifconfig', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = SP.communicate() for line in str(stdout, 'utf-8').split('n'): if re.match('^s*ethers+(S+)s+.*$', line): my_match = re.match('^s*ethers+(S+)s+.*$', line) mac = my_match.group(1) break return macif __name__ == '__main__': # check license file check_license_file().license_check() 3.把解析脚本转换成.so文件

setup.py,

# -*- coding: UTF-8 -*-from distutils.core import setupfrom Cython.Build import cythonizepy_files = ['check_license_file.py', ]setup(ext_modules=cythonize(py_files), )

执行脚本python setup.py build_ext --inplace生成文件,把其中的.so文件该名成check_license_file.so

4. 使用license

test.py
from utils.license.check_license_file import check_license_file

check_license_file().license_check()
a = input(‘请输入数字:’)
print(a)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。