如何使用 python 在 nginx 中创建安全链接模块的链接?我希望使用 nginx 来提供具有过期链接的安全文件。 链接到 Nginx 维基
来自 shadfc 答案的代码有效。对于Python 3,需要进行一些修改:
import base64
import hashlib
import calendar
import datetime
secret = "itsaSSEEECRET"
url = "/secure/email-from-your-mom.txt"
future = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
expiry = calendar.timegm(future.timetuple())
secure_link = f"{secret}{url}{expiry}".encode('utf-8')
hash = hashlib.md5(secure_link).digest()
base64_hash = base64.urlsafe_b64encode(hash)
str_hash = base64_hash.decode('utf-8').rstrip('=')
print(f"{url}?st={str_hash}&e={expiry}")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4941 次 |
最近记录: |