• Python修饰器 ,控制授权,通过ini配置文件 使用密钥 给函数限制试用期和过期后试用次数


     Python脚本可以通过 pyinstaller 等打包成exe分发给别人使用,如何控制自己的包不被‘盗版’呢?

    通过增加装饰器,网络验证,本地配置文件都通过的话才可使用

     1 # -*- coding:utf-8 -*-
     2 import ConfigParser#, os
     3 import requests
     4 import re
     5 import pandas as pd #习惯性用了pandas的日期模块
     6 
     7 
     8 class Initconfig:
     9     """read,write config ini file, default relative path :config.ini
    10         get_value : if ini not exist,will make one
    11         set_value : modefy one variable and close; not fit for a lot of variables
    12     """
    13     def __init__(self, filename = None):
    14         if not filename:
    15             filename = 'myconfig.ini'
    16         self.filename = filename
    17         self.cfg = ConfigParser.ConfigParser()
    18 
    19     def get_value(self, Section, Key, Default=""):
    20         self.cfg.read(self.filename)
    21         try:
    22             value = self.cfg.get(Section, Key)
    23         except:
    24             value = Default
    25         return value
    26 
    27     def set_value(self, Section, Key, Value):
    28         with open(self.filename, 'w+') as fp:
    29             if not self.cfg.has_section(Section):
    30                 self.cfg.add_section(Section)  
    31             self.cfg.set(Section, Key, Value)
    32             self.cfg.write(fp)
    33 
    34 
    35 def verify_blog_key():
    36     xx = requests.get('http://www.cnblogs.com/willowj/p/7170767.html')
    37     key = re.findall('"key" = (w+)', xx.text)
    38     xx.connection.close()
    39     if key:
    40         key = key[-1]
    41         if key == 'asfklbzxchjgd':
    42             return True
    43 
    44 
    45 def use_ctrl(config_file):
    46     '''wraper func with  each config ini file'''
    47     def _ctrl(func):
    48         def verifed(*arg):          #控制 有效期
    49             if pd.datetime.now() < pd.datetime(2017, 7, 11) or verify_blog_key(): # 网络控制密钥
    50                 return func(*arg) 
    51             
    52             myini = Initconfig(filename=config_file)
    53             if myini.get_value('key', 'key') == 'asfklbzxchjgd' :
    54                 return func(*arg)   #配置文件 密钥
    55             
    56             times = myini.get_value('key', 'time') # 试用次数
    57             if not times:
    58                 myini.set_value('key', 'time', 1)
    59                 times = '1'
    60             if times.isdigit() and int(times) < 6:
    61                 myini.set_value('key', 'time',int(times)+1)
    62                 return func(*arg)
    63             
    64             print 'pakage tools liscence expired, please contact distributor'
    65     
    66         return verifed
    67     return _ctrl
    68 
    69 
    70 if __name__ == '__main__':
    71     @use_ctrl('use_ctrl_test.ini')
    72     def test_func():
    73         print 'ok'
    74         
    75     for x in range(7):
    76         test_func()
    ok
    ok
    ok
    ok
    ok
    pakage tools liscence expired, please contact distributor
    pakage tools liscence expired, please contact distributor

    时间,网络密钥,本地配置文件密钥 都不通过的情况下,执行5次后,无法再执行了。

      ini配置文件:

      [key]
      time = 5

     漏洞是,每删除配置文件一次就能用5次

    ----------------》密钥:

    "key" = asfklbzxchjgd

    "key" = asfklbzxchjgd

  • 相关阅读:
    sysbench安装部署for postgresql
    sysbench安装部署for mysql
    pg创建dblink访问另外的库下的表
    11grac配置EM
    重新配置grid(ocr有备份,采用ocr备份进行恢复)
    rac资源显示脚本
    重新配置grid(已经有数据库的情况)ocr没有备份
    Can't locate Env.pm in @INC
    比特币-隔离见证(Segwit)
    传智播客 .NET面试宝典(2014版)
  • 原文地址:https://www.cnblogs.com/willowj/p/7170767.html
Copyright © 2020-2023  润新知