API三级验证
裝AES API驗(yàn)證模塊
python 3.5 pip3 install wheel ?進(jìn)入目錄: ??? pip3 install pycrypto-2.6.1-cp35-none-win32.whl
python 3.6 pip3 install pycryptodome
?
client
import time import requests import hashlibctime = time.time() key = "asdfasdfasdfasdf098712sdfs" new_key = "%s|%s" %(key,ctime,) m = hashlib.md5() m.update(bytes(new_key,encoding='utf-8')) md5_key = m.hexdigest() md5_time_key = "%s|%s" %(md5_key,ctime)response = requests.get("http://127.0.0.1:8000/api/asset.html",headers={'OpenKey':md5_time_key}) print(response.text)?
?
server
import time from django.shortcuts import render,HttpResponse from repository import models from django.conf import settings # redis/Memcache 緩存工具 api_key_record = {# "1b96b89695f52ec9de8292a5a7945e38|1501472467.4977243":1501472477.4977243 } def asset(request):client_md5_time_key = request.META.get('HTTP_OPENKEY')client_md5_key,client_ctime = client_md5_time_key.split('|')client_ctime = float(client_ctime)server_time = time.time()# 第一關(guān)if server_time-client_ctime > 10:return HttpResponse('【第一關(guān)】小伙子,別唬我,太長了')# 第二關(guān)temp = "%s|%s" %(settings.AUTH_KEY,client_ctime,)m = hashlib.md5()m.update(bytes(temp,encoding='utf-8'))server_md5_key = m.hexdigest()if server_md5_key != client_md5_key:return HttpResponse('【第二關(guān)】小子,你是不是修改時(shí)間了')for k in list(api_key_record.keys()):v = api_key_record[k]if server_time > v:del api_key_record[k]# 第三關(guān):if client_md5_time_key in api_key_record:return HttpResponse('【第三關(guān)】有人已經(jīng)來過了...')else:api_key_record[client_md5_time_key] = client_ctime + 10 return HttpResponse('認(rèn)證成功')?
轉(zhuǎn)載于:https://www.cnblogs.com/domestique/p/7714451.html
總結(jié)
- 上一篇: Python之创建tuple
- 下一篇: 洛谷 1195 口袋的天空