码迷,mamicode.com
首页 > Windows程序 > 详细

api校验

时间:2019-09-04 13:14:21      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:mat   digest   ash   gen   port   post   date   data   ges   

服务端代码:

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)

 

api校验

标签:mat   digest   ash   gen   port   post   date   data   ges   

原文地址:https://www.cnblogs.com/guniang/p/11458316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!