網站首頁 編程語言 正文
程序員可能會私下給別人開發一些工具,但是通常要給別人試用一下,但是萬一別人試用后,把你拉黑,那就白忙活了。今天就分享如何在 Python 里設置有效期。
為了設置有效期,首先要獲取當前時間,如果獲取本地時間,那么別人可以通過修改操作系統的時間來一直保持有效。最好的辦法就是獲取網絡時間。你說他斷網怎么辦?
如果斷網,有兩種方法,要么不給用,要么再取本地時間。
1、獲取網絡時間
如何獲取網絡時間呢,可以從 HTTP 返回的 Header 里獲取時間,由于這個時間是 GMT 格式的 UTC 時間,我們還需要轉為本地時間,下面的腳本都考慮到了,均采用標準庫。
from?datetime?import?datetime
from?urllib.request?import?urlopen
import?time
import?ssl
import?sys
ssl._create_default_https_context?=?ssl._create_unverified_context
def?get_network_time():
????url?=?"https://www.baidu.com"
????try:
????????with?urlopen(url)?as?res:
????????????utctime?=?gmtstr_to_localtime(res.getheader("Date"))
????????????return?datetime_from_utc_to_local(utctime)
????except?Exception?as?e:
????????print("請檢查網絡設置")
????????sys.exit()
def?gmtstr_to_localtime(gmtstr)?->?datetime:
????return?datetime.strptime(gmtstr,?"%a,?%d?%b?%Y?%H:%M:%S?GMT")
def?datetime_from_utc_to_local(utc_datetime):
????now_timestamp?=?time.time()
????offset?=?datetime.fromtimestamp(now_timestamp)?-?datetime.utcfromtimestamp(
????????now_timestamp
????)
????return?utc_datetime?+?offset
if?__name__?==?"__main__":
????print(get_network_time())
????#?Output:
????#?2022-07-19?06:39:12
2、上鎖
這一步很簡單,一個日期判斷就搞定了:
def?lock():
????now?=?get_network_time()
????end_date?=?datetime(2022,?8,?26)
????if?now?>?end_date:
????????print("試用已到期")
????????sys.exit()
然后把上面獲取網絡時間的代碼及 lock 函數均放在 lock.py 文件里,在我們的核心模塊 core_work.py 中導入:
from?lock?import?lock
def?somefunc():
????lock()
????#?do?your?work
在需要 lock() 的地方調用下 lock 函數就可以了。
你可能還需要一個 startup.py 腳本來啟動整個程序:
from?core_work?import?main
main()
3、編譯 pyd
不編譯的話,或者編譯為 pyc 的話,上面的工作等于沒做,pyc 可以直接反編譯成 py 文件,連命名符號都不變。
編譯的話,只需要需要把獲取網絡時間的代碼,lock 函數的定義及調用 lock 的代碼都編譯成 pyd,本例子中就是 lock.py 和 core_work.py 編譯成 lock.pyd 和 core_work.pyd,然后把源代碼 lock.py 和 core_work.py 刪除,其實就可以發布了,想破解只能反編譯 pyd 文件,得到的是匯編代碼,這個門檻已經拒絕了大多數的人。
pyd 的編譯流程是 .py -> .c -> pyd,也就是說 pyd 是從 c 語言編譯的,除了隱藏源代碼之外,速度也會變快一些。
如果不放心的話,那就 pyinstall -k key startup.py 再加密打包一下,key 就是加密的密碼,如果在 pyd 文件 import 了第三方庫,那么要在 spec 文件中填寫 hiddenimports。
如何編譯 pyd 呢?代碼如下:
from?distutils.core?import?setup
from?Cython.Build?import?cythonize
files?=?["lock.py",?"core_work.py"]
setup(
????name="yourapp",
????ext_modules=cythonize(files),
????script_args=["build_ext",?"-b",?"./build",?"-t",?"./build/temp"],
)
運行之前你要 pip install cython。
然后在 build 文件夾就可以看到編譯好的 pyd 文件,然后把他們移動到源代碼的位置,重命名為 xxx.pyd 就可以了。
原文鏈接:https://mp.weixin.qq.com/s/-TBM2PUFwsZJFC0egS17IQ
相關推薦
- 2022-08-20 docker鏡像alpine中安裝oracle客戶端_docker
- 2022-05-17 Spring boot 集成Redis客戶端Lettuce,導致服務線程數不斷增加
- 2022-10-28 ReactDOM?隱藏特性詳解_React
- 2022-08-06 .Net?Core中使用EFCore生成反向工程_實用技巧
- 2023-05-22 Redis數據結構類型示例解析_Redis
- 2022-09-05 Go語言接口的用法詳解_Golang
- 2024-01-31 nginx配置文件中最后一個 include servers/*;作用是什么?
- 2022-07-28 pytest使用parametrize將參數化變量傳遞到fixture_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支