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.