API

API

1.在请求头中加入key

server 给客户端发一个key 

client (key)

服务端在client请求中看是否有key,如果有就是正常客户端没有就是非正常客户端

 

client.py
import requests
key="qwrwertyuiop"

response = requests.get("http://127.0.0.1:8000/api/test.html",header={'auth-api':key})
print(response.text)
server.py
from django.http import HttpResponse

key="qwrwertyuiop"
def test(request):
    client_key=request.META.get("HTTP_AUTH_API")
    if client_key == key:
        return HttpResponse("you get me")
    else:
        return HttpResponse("no")

2. md5和时间,请求头中的值动态起来

客户端给服务器发过去,服务端动态的解出来

思路--》时间是动态的,但是key不是动态的 两个需要配合起来,但不能拼接其他,如果简单拼接key就泄漏了---》把key和time md5

client.py

import requests
key="qwrwertyuiop"
import hashlib
import time




def md5(arg):
    hs=hashlib.md5() #由于在python3中在update用的是字节类型所以需要把字符串变成字节
    hs.update(arg.encode("utf8"))
    return hs.hexdigest()

ctime = str(time.time())
new_key = "%s|%s" %(key,ctime,)
md5_str=md5(new_key)
#只发随机字符串服务端无法解 ,所以需要把时间发给服务端,试服务端即有key也有时间,服务端也同样加密,加密后的值和客户端一致说明正常用户
api_header_val="%s|%s" %(md5_str,ctime,)
# response = requests.get("http://127.0.0.1:8000/api/test.html",header={'auth-api':md5_str})
response = requests.get("http://127.0.0.1:8000/api/test.html",header={'auth-api':api_header_val})
print(response.text)
server.py

import hashlib

from django.http import HttpResponse

key="qwrwertyuiop"

def md5(arg):
    hs=hashlib.md5() #由于在python3中在update用的是字节类型所以需要把字符串变成字节
    hs.update(arg.encode("utf8"))
    return hs.hexdigest()

def test(request):
    auth_header_val=request.META.get("HTTP_AUTH_API")
    client_md5_str,client_ctime=auth_header_val.split("|",maxsplit=1)
    server_md5_str = md5("%s|%s" %(key,client_ctime,))
    if client_md5_str == server_md5_str:
        return HttpResponse("you get me")
    else:
        return HttpResponse("no")

  

3. (2)会产生大量可用的key--》时间限制

server.py
import hashlib

import time
from django.http import HttpResponse

key="qwrwertyuiop"

def md5(arg):
    hs=hashlib.md5() #由于在python3中在update用的是字节类型所以需要把字符串变成字节
    hs.update(arg.encode("utf8"))
    return hs.hexdigest()

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("you get me")
    else:
        return HttpResponse("no")
client.py
import requests
key="qwrwertyuiop"
import hashlib
import time




def md5(arg):
    hs=hashlib.md5() #由于在python3中在update用的是字节类型所以需要把字符串变成字节
    hs.update(arg.encode("utf8"))
    return hs.hexdigest()

ctime = str(time.time())
new_key = "%s|%s" %(key,ctime,)
md5_str=md5(new_key)
#只发随机字符串服务端无法解 ,所以需要把时间发给服务端,试服务端即有key也有时间,服务端也同样加密,加密后的值和客户端一致说明正常用户
api_header_val="%s|%s" %(md5_str,ctime,)
# response = requests.get("http://127.0.0.1:8000/api/test.html",header={'auth-api':md5_str})
response = requests.get("http://127.0.0.1:8000/api/test.html",header={'auth-api':api_header_val})
print(response.text)

  

3.添加加密规则限制【字符串不能被修改】,访问过的key和时间添加到字典中,不允许在访问

server.py
import hashlib

import time
from django.http import HttpResponse

key="qwrwertyuiop"
visited_key={
    #'sf':time
}

def md5(arg):
    hs=hashlib.md5() #由于在python3中在update用的是字节类型所以需要把字符串变成字节
    hs.update(arg.encode("utf8"))
    return hs.hexdigest()

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("正常用户")

  

client.py
import requests
key="qwrwertyuiop"
import hashlib
import time




def md5(arg):
    hs=hashlib.md5() #由于在python3中在update用的是字节类型所以需要把字符串变成字节
    hs.update(arg.encode("utf8"))
    return hs.hexdigest()

ctime = str(time.time())
new_key = "%s|%s" %(key,ctime,)
md5_str=md5(new_key)
#只发随机字符串服务端无法解 ,所以需要把时间发给服务端,试服务端即有key也有时间,服务端也同样加密,加密后的值和客户端一致说明正常用户
api_header_val="%s|%s" %(md5_str,ctime,)
# response = requests.get("http://127.0.0.1:8000/api/test.html",header={'auth-api':md5_str})
response = requests.get("http://127.0.0.1:8000/api/test.html",header={'auth-api':api_header_val})
print(response.text)

  

  

  

  

  

  

原文地址:https://www.cnblogs.com/morgana/p/7618306.html