How to generate a nginx secure link in python

前端 未结 3 1316
迷失自我
迷失自我 2021-01-17 06:41

How do i make the link for the secure link module in nginx using python? I\'m looking to use nginx to serve secured files that have expiring links. Link to Nginx Wiki

3条回答
  •  悲&欢浪女
    2021-01-17 07:37

    import base64
    import hashlib
    
    future = datetime.datetime.now() + datetime.timedelta(minutes=5)
    url = "/securedir/file.txt"
    timestamp = str(time.mktime(future.timetuple()))
    security = base64.b64encode(hashlib.md5( secret ).digest()).replace('+', '-').replace('/', '_').replace("=", "")
    data = str(url) + "?st=" + str(security) + "&e=" + str(timestamp)
    

    data is your generated url of the form:

    /securedir/file.txt?st=PIrEk4JX5gJPTGmvqJG41g&e=1324527723
    

提交回复
热议问题