七 解析器

解析器的作用:

-用来解析前台传过来的数据编码方式
urlencoded:form表单:name=lqz&age=
formdata :上传文件:--dadfgdgag--
json:json格式 {"name":"lqz"}
-解析器取的顺序
视图类中
django总settings里取
drf默认的配置文件取
—全局配置
在setting中:
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES':[
'rest_framework.parsers.JSONParser',
# 'rest_framework.parsers.FormParser',
] }
-局部使用:
在视图类中:
from rest_framework.parsers import JSONParser,MultiPartParser,FormParser
parser_classes = [JSONParser,FormParser]

例子

解析器
局部配置
from rest_framework.parsers import JSONParser
class Book(APIView):
parser_classes = [JSONParser, ]
def get(self,request,*args,**kwargs):
return HttpResponse('ok')
def post(self,request):
print(request.data)
return HttpResponse('post') from rest_framework.parsers import JSONParser,MultiPartParser,FormParser
class Book(APIView):
parser_classes = [JSONParser,FormParser]
def get(self,request,*args,**kwargs):
return HttpResponse('ok')
def post(self,request):
print(request.data)
return HttpResponse('post')

八、认证组件

1、校验是否登陆

    -作用:校验是否登陆
-首先定义一个类,继承BaseAuthentication,写一个方法:authenticate,在方法内部,实
证过程,认证通过,返回None或者两个对象(user,auth),这两个对象,在视图类的request中可以取出来
from rest_framework.authentication import BaseAuthentication
class myAuthen(BaseAuthentication):
def authenticate(self, request):
token = request.query_params.get('token')
ret = models.UserToken.objects.filter(token=token).first()
if ret:
# return ret.user, ret
# 要写多个认证类,这个地返回None
# 最后一个认证类,返回这俩值
return ret.user, ret
else:
raise AuthenticationFailed('您没有登陆') -局部使用:在视图类中(可以写多个)
authentication_classes = [myAuthen, ]
-全局使用:
"DEFAULT_AUTHENTICATION_CLASSES":["app01.service.auth.Authentication",]

2、正常非drf认证代码,update_or_create意思是有则更新,无则创建

class User(models.Model):
nid = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
pwd=models.CharField(max_length=32,null=True)
mychoice=((1,'普通用户'),(2,'超级用户'),(3,'宇宙用户'))
usertyle=models.IntegerField(choices=mychoice,default=1) class UserToken(models.Model):
user=models.OneToOneField(to=User,to_field='nid')
token=models.CharField(max_length=64)

def get_token(username):
import hashlib
import time
md = hashlib.md5()
# update内必须传bytes格式
md.update(username.encode('utf-8'))
md.update(str(time.time()).encode('utf-8'))
return md.hexdigest() class Login(APIView):
def post(self, request):
response = MyResponse()
name = request.data.get('name')
pwd = request.data.get('pwd')
user = models.User.objects.filter(name=name, pwd=pwd).first()
if user:
response.msg = '登陆成功'
# 登陆成功,返回一个随机字符串,以后在发请求,都携带这个字符串
token = get_token(name)
response.token = token
# 把随机字符串保存到数据库,有就更新,没有就创建
# ret=models.UserToken.objects.update_or_create(user_id=user.id,kwargs={'token':token})
ret = models.UserToken.objects.update_or_create(user=user, defaults={'token': token}) else:
response.msg = '用户名或密码错误'
response.status =
return JsonResponse(response.get_dic)

3、drf的认证组件用法,return user或者auth

class myAuthen():
def authenticate(self,request):
token = request.query_params.get('token')
ret = models.UserToken.objects.filter(token=token).first()
if ret:
return ret.user,ret
else:
raise AuthenticationFailed('您咩有登陆') def authenticate_header(self,value):
pass from rest_framework.request import Request
from app01 import myserial
class Book(APIView):
authentication_classes = [myAuthen, ]
def get(self, request):
response = MyResponse()
print(request.user.name)
print(request.auth.token)
# 必须登陆才能访问
books = models.Book.objects.all()
ret = myserial.BookSer(instance=books, many=True)
response.msg = '查询成功'
response.data = ret.data
return JsonResponse(response.get_dic, safe=False)

4、先建个auth.py,存入认证组价的功能

from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from app01 import models class myAuthen(BaseAuthentication):
def authenticate(self, request):
token = request.query_params.get('token')
ret = models.UserToken.objects.filter(token=token).first()
if ret:
# return ret.user, ret
# 要写多个认证类,这个地返回None
# 最后一个认证类,返回这俩值
return ret.user, ret
else:
raise AuthenticationFailed('您没有登陆')

5、若在setting里配置了全局验证,则全局验证,但是视图函数里的login()不需要,则我们参考了顺序是先找局部,在找setting里。所以需要在视图函数里配置

authentication_classes = [],即不需要验证
class Login(APIView):
authentication_classes = []
def post(self, request):
response = MyResponse()
name = request.data.get('name')
pwd = request.data.get('pwd')
user = models.User.objects.filter(name=name, pwd=pwd).first()
if user:
response.msg = '登陆成功'
# 登陆成功,返回一个随机字符串,以后在发请求,都携带这个字符串
token = get_token(name)
response.token = token
# 把随机字符串保存到数据库,有就更新,没有就创建
# ret=models.UserToken.objects.update_or_create(user_id=user.id,kwargs={'token':token})
ret = models.UserToken.objects.update_or_create(user=user, defaults={'token': token}) else:
response.msg = '用户名或密码错误'
response.status =
return JsonResponse(response.get_dic)

6、settings

REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES':[
'rest_framework.parsers.JSONParser',
# 'rest_framework.parsers.FormParser',
],
"DEFAULT_AUTHENTICATION_CLASSES": ["app01.auth.myAuthen", ],
"DEFAULT_PERMISSION_CLASSES":["app01.auth.myPermission",] }

7、存的token数据一般存在服务器端数据库里,但是存在压力,第一存在redis里,第二种方式就是不存数据库的token验证,及如下所示

def get_token(id,salt=''):
import hashlib
md=hashlib.md5()
md.update(bytes(str(id),encoding='utf-8'))
md.update(bytes(salt,encoding='utf-8')) return md.hexdigest()+'|'+str(id) def check_token(token,salt=''):
ll=token.split('|')
import hashlib
md=hashlib.md5()
md.update(bytes(ll[-],encoding='utf-8'))
md.update(bytes(salt,encoding='utf-8'))
if ll[]==md.hexdigest():
return True
else:
return False class TokenAuth():
def authenticate(self, request):
token = request.GET.get('token')
success=check_token(token)
if success:
return
else:
raise AuthenticationFailed('认证失败')
def authenticate_header(self,request):
pass
class Login(APIView):
def post(self,reuquest):
back_msg={'status':,'msg':None}
try:
name=reuquest.data.get('name')
pwd=reuquest.data.get('pwd')
user=models.User.objects.filter(username=name,password=pwd).first()
if user:
token=get_token(user.pk)
# models.UserToken.objects.update_or_create(user=user,defaults={'token':token})
back_msg['status']=''
back_msg['msg']='登录成功'
back_msg['token']=token
else:
back_msg['msg'] = '用户名或密码错误'
except Exception as e:
back_msg['msg']=str(e)
return Response(back_msg)
from rest_framework.authentication import BaseAuthentication
class TokenAuth():
def authenticate(self, request):
token = request.GET.get('token')
token_obj = models.UserToken.objects.filter(token=token).first()
if token_obj:
return
else:
raise AuthenticationFailed('认证失败')
def authenticate_header(self,request):
pass class Course(APIView):
authentication_classes = [TokenAuth, ] def get(self, request):
return HttpResponse('get') def post(self, request):
return HttpResponse('post')

九 权限组件

1、校验用户是否有权限访问

-作用:校验用户是否有权限访问
-因为是在认证通过才执行,所以可以取出user
class myPermission():
message = '不是超超级用户,查看不了'
def has_permission(self, request, view):
if request.user.usertyle != :
return False
else:
return True
-局部使用
在视图类中:permission_classes=[myPermission,]
-全局使用
在setting中:
"DEFAULT_PERMISSION_CLASSES":["app01.auth.myPermission",]

2、choices=的写法,一般用性别等不常用的常识

class User(models.Model):
nid = models.AutoField(primary_key=True)
name = models.CharField(max_length=)
pwd=models.CharField(max_length=,null=True)
mychoice=((,'普通用户'),(,'超级用户'),(,'宇宙用户'))
usertyle=models.IntegerField(choices=mychoice,default=) class UserToken(models.Model):
user=models.OneToOneField(to=User,to_field='nid')
token=models.CharField(max_length=)

3、eg

from rest_framework.permissions import BasePermission
class myPermission(BasePermission):
message = '不是超超级用户,查看不了'
def has_permission(self, request, view):
if request.user.usertyle != :
return False
else:
return True
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES':[
'rest_framework.parsers.JSONParser',
# 'rest_framework.parsers.FormParser',
],
"DEFAULT_AUTHENTICATION_CLASSES": ["app01.auth.myAuthen", ],
"DEFAULT_PERMISSION_CLASSES":["app01.auth.myPermission",] }
from app01.auth import myAuthen
from app01.auth import myPermission class Book(APIView):
# authentication_classes = [myAuthen, ]
# permission_classes=[myPermission,] def get(self, request):
response = MyResponse()
print(request.user.name)
print(request.auth.token)
# 必须登陆才能访问
books = models.Book.objects.all()
ret = myserial.BookSer(instance=books, many=True)
response.msg = '查询成功'
response.data = ret.data
return JsonResponse(response.get_dic, safe=False)

十、频率组件

1、参考原码写的符合频率组件logic的代码

class MyThro():
VISIT_RECORD = {}
# VISIT_RECORD = {ip:[时间1,时间2],ip2:[],ip3:[当前时间]} def __init__(self):
self.history = None def allow_request(self, request, view):
# ()取出访问者ip
# print(request.META)
ip = request.META.get('REMOTE_ADDR')
import time
ctime = time.time()
# ()判断当前ip不在访问字典里,添加进去,并且直接返回True,表示第一次访问
if ip not in self.VISIT_RECORD:
self.VISIT_RECORD[ip] = [ctime, ]
return True
self.history = self.VISIT_RECORD.get(ip)
# ()循环判断当前ip的列表,有值,并且当前时间减去列表的最后一个时间大于60s,把这种数据pop掉,这样列表中只有60s以内的访问时间,
while self.history and ctime - self.history[-] > :
self.history.pop()
# ()判断,当列表小于3,说明一分钟以内访问不足三次,把当前时间插入到列表第一个位置,返回True,顺利通过
# ()当大于等于3,说明一分钟内访问超过三次,返回False验证失败
if len(self.history) < :
self.history.insert(, ctime)
return True
else:
return False def wait(self):
import time
ctime = time.time()
return - (ctime - self.history[-])

视图函数使用代码

from app01.auth import MyThro
class Book(APIView):
authentication_classes = [myAuthen, ]
permission_classes=[]
throttle_classes=[MyThro,] def get(self, request):
# request.user
response = MyResponse()
# request.GET
print(request.user.name)
print(request.auth.token)
# 必须登陆才能访问
books = models.Book.objects.all()
ret = myserial.BookSer(instance=books, many=True)
response.msg = '查询成功'
response.data = ret.data
return JsonResponse(response.get_dic, safe=False)

2、使用

    频率:
-定义一个类:
from rest_framework.throttling import SimpleRateThrottle
class MyThro(SimpleRateThrottle):
scope = 'aaa'
def get_cache_key(self, request, view):
# return self.get_ident(request)
return request.META.get('REMOTE_ADDR') -setting中配置
"DEFAULT_THROTTLE_RATES":{
'aaa':'3/msssssss'
} -全局使用:
'DEFAULT_THROTTLE_CLASSES':['app01.auth.MyThro',],
-局部使用:
throttle_classes=[MyThro,]

eg.

from rest_framework.throttling import SimpleRateThrottle
class MyThro(SimpleRateThrottle):
scope = 'aaa'
def get_cache_key(self, request, view):
# return self.get_ident(request)
return request.META.get('REMOTE_ADDR')
# return

3、假如全局限制每分钟查看3次,视图函数每分钟10次,则需要在

"DEFAULT_THROTTLE_RATES":{
'aaa':'3/msssssss'
},

from rest_framework.throttling import SimpleRateThrottle
class MyThro(SimpleRateThrottle):
scope = 'aaa'
def get_cache_key(self, request, view):
# return self.get_ident(request)
return request.META.get('REMOTE_ADDR')
# return
将aaa 赋值为10/mmmmms

RESTful规范(二)的更多相关文章

  1. 一、restful规范 二、CBV(View)源代码执行流程 三、drf框架安装和简单使用

    一.restful规范 ''' 它是一个规范,面向资源架构 十条规范 1.API与用户的通讯协议,总是使用HTTPs协议,确保了网络传输的安全性 2.域名 --https://api.example. ...

  2. RESTful规范

    一. 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角 ...

  3. DjangoRestFramework 学习之restful规范 APIview 解析器组件 Postman等

    DjangoRestFramework学习一之restful规范.APIview.解析器组件.Postman等 本节目录 一 预备知识 二 restful规范 三 DRF的APIView和解析器组件 ...

  4. RESTful规范1

    RESTful规范 一 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为"表征状 ...

  5. Django restful 规范

    一.REST Frame Work REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为"表征状态转移&q ...

  6. drf1 rest & restful规范

    web服务交互 我们在浏览器中能看到的每个网站,都是一个web服务.那么我们在提供每个web服务的时候,都需要前后端交互,前后端交互就一定有一些实现方案,我们通常叫web服务交互方案. 目前主流的三种 ...

  7. day89 DjangoRsetFramework学习---restful规范,解析器组件,Postman等

     DjangoRsetFramework学习---restful规范,解析器组件,Postman等           本节目录 一 预备知识 二 restful规范 三 DRF的APIView和解析 ...

  8. 18.DjangoRestFramework学习一之restful规范、APIview、解析器组件、Postman等

    一 预备知识 预备知识:django的CBV和FBV CBV(class based view):多用,简单回顾一下 FBV(function based view): CBV模式的简单操作:来个登陆 ...

  9. Django框架(十七)-- CBV源码分析、restful规范、restframework框架

    一.CBV源码分析 1.url层的使用CBV from app01 import views url(r'book/',views.Book.as_view) 2.as_view方法 as_view是 ...

  10. 基于Django的Rest Framework框架的RESTful规范研究

    一.什么是RESTful规范 总的来说:RESTful规范就是一个非技术,人为约定的一个面向资源的架构理念. REST与技术无关,代表的是一种软件架构风格,REST是Representational ...

随机推荐

  1. nyoj 0325 zb的生日(dp)

    nyoj 0325 zb的生日 zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集 ...

  2. 【Java算法】输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

    import java.util.Scanner; public class CountZimuShuzi { public static void main(String[] args) { Sys ...

  3. 查找xml中的接口名及涉及表名并输出

    #! /usr/bin/env python3 # -*- coding:utf-8 -*- import xml.dom.minidom  #该模块被用来处理xml文件 import re #正则表 ...

  4. Java的第一个晞月自己打的程序

    1.编写一个程序,求1!+2!+…+10!的值. package xxx; public class abc { public static void main(String args[]) { in ...

  5. Office 365 企业应用以及服务概览 分享记录

    博客地址:http://blog.csdn.net/FoxDave 分享时间: 2017年9月14日 分享地点: 部门内部 参与人数: 16人 分享内容: 讲解微软MVP项目计划的相关内容:讲解O ...

  6. SharePoint online Multilingual support - Development(1)

    博客地址:http://blog.csdn.net/FoxDave 上一节讲了SharePoint Online网站多语言的实现原理机制,本节主要从编程的角度来谈一下如何进行相关的设置. 下面列出 ...

  7. DataTable数据统计方法

    调用方法: public object Compute(string strExpression,string strFilter) 参数说明: strExpression:要计算的表达式字符串,基本 ...

  8. C点滴成海----函数声明、函数定义、函数原型

    一.函数声明 1.格式 函数体去掉函数定义中的内容再加上分号,如下所示: 返回值类型 函数名( 类型 形参, 类型 形参… ); 返回值类型 函数名( 类型, 类型…); 2.特点 函数声明只是对编译 ...

  9. <context:annotation-config/>和<mvc:annotation-driven/>及解决No mapping found for HTTP request with URI [/role/getRole] in DispatcherServlet with name 'springmvc-config'

    1:什么时候使用<context:annotation-config> 当你使用@Autowired,@Required,@Resource,@PostConstruct,@PreDest ...

  10. phpexcel 的使用

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...