• 装饰器api


    import hashlib
    
    import time
    from django.http import HttpResponse
    
    key="qwrwertyuiop"
    visited_key={
        #'key':time
    }
    
    
    def api_auth(func):
        def inner(request,*args,**kwargs):
            server_float_ctime = time.time()
            auth_header_val = request.META.get("HTTP_AUTH_API")
            client_md5_str, client_ctime = auth_header_val.split("|", maxsplit=1)
            client_float_ctime = float(client_ctime)
            # 第一关时间
            if (client_float_ctime + 10) < server_float_ctime:
                return HttpResponse("时间太久了")
            # 第二关加密
            server_md5_str = md5("%s|%s" % (key, client_ctime,))
    
            if client_md5_str != server_md5_str:
                return HttpResponse("休想")
    
            # 第三关
            if visited_key.get(client_md5_str):
                return HttpResponse("you are late")
    
            visited_key[client_md5_str] = client_float_ctime
    
            return func(request,*args,**kwargs)
        return inner
    
    
    def md5(arg):
        hs=hashlib.md5() #由于在python3中在update用的是字节类型所以需要把字符串变成字节
        hs.update(arg.encode("utf8"))
        return hs.hexdigest()
    
    @api_auth
    def test(request):
        # server_float_ctime = time.time()
        # auth_header_val=request.META.get("HTTP_AUTH_API")
        # client_md5_str,client_ctime=auth_header_val.split("|",maxsplit=1)
        # client_float_ctime=float(client_ctime)
        # #第一关时间
        # if (client_float_ctime+10) < server_float_ctime:
        #     return HttpResponse("时间太久了")
        # # 第二关加密
        # server_md5_str = md5("%s|%s" %(key,client_ctime,))
        # 
        # if client_md5_str != server_md5_str:
        #     return HttpResponse("休想")
        # 
        # #第三关
        # if visited_key.get(client_md5_str):
        #     return HttpResponse("you are late")
        # visited_key[client_md5_str]=client_float_ctime
        return  HttpResponse("正常用户")
    

      

  • 相关阅读:
    禅道环境搭建
    java web自动化js控制滚动条
    Selenium启动项参数设置
    模糊定位
    eclipse设置UTF-8
    eclipse在线安装testNG
    3步在Linux虚拟机搭建Hadoop
    Linux系统下shell脚本中执行sql
    Linux操作系统中记录操作记录的地方
    awk命令看这一篇就够了
  • 原文地址:https://www.cnblogs.com/morgana/p/7624762.html
Copyright © 2020-2023  润新知