zoukankan      html  css  js  c++  java
  • api校验

    服务端代码:
    
    import hashlib
    import time
    KEY = 'RTYUIFGHJKVBNM'
    
    
    def gen_key(ctime):
        md5 = hashlib.md5()
        key = "{}|{}".format(KEY,ctime)
        md5.update(key.encode('utf-8'))
        return md5.hexdigest()
    
    
    class TestView(APIView):
        def post(self,request):
            print(request.data)
            # print(request._request.GET.get('KEY'))
            key = request.query_params.get('key')
            ctime = request.query_params.get('time')
            now = time.time()
            if now - float(ctime) > 3:
                return Response('校验失败,Key超时')
    
            if key != gen_key(ctime):
                return  Response('校验失败')
    
            return Response('ok')
    客户端代码:
    
    import requests
    import time
    import hashlib
    
    KEY = 'RTYUIFGHJKVBNM'
    data = {'k1':'v1'}
    
    def gen_key(ctime):
        md5 = hashlib.md5()
        key = "{}|{}".format(KEY,ctime)
        md5.update(key.encode('utf-8'))
        return md5.hexdigest()
    
    ctime = time.time()
    key = gen_key(ctime)
    time.sleep(3)
    ret = requests.post(
        url='http://127.0.0.1:8000/app01/test/',
        data = data,
        params = {'key':key,'time':ctime}
    )
    print(ret.text)
    We are down, but not beaten. tested but not defeated.
  • 相关阅读:
    day02-xml
    day01-java重点复习
    RPM包和YUM仓库管理
    Nginx的下载与安装
    yum源本地部署完后网络部署报错
    RAID和LVM
    磁盘管理
    xargs详解
    locate及find查找命令
    linux的进程和管道符(二)
  • 原文地址:https://www.cnblogs.com/guniang/p/11458316.html
Copyright © 2011-2022 走看看