澳门新蒲京娱乐

新蒲京官方下载 7
小软件部分

Async方法死锁的问题

Restframework框架总结及restful规范,rest_Framework框架认证源码分析

-分工明确,提高效率
-对于后端人员,主要为前端提供:API接口
 以前的你的接口:
  http://127.0.0.1:8000/index/
  http://127.0.0.1:8000/users/
  http://127.0.0.1:8000/add_users/
  http://127.0.0.1:8000/del_users/
  http://127.0.0.1:8000/edit_users/
 restful 规范:
  http://127.0.0.1:8000/users/ 
-同样的业务逻辑,能开发多个端口(app,网页端,ios端,微信小程序等),统一一套API接口,能适用于多个端口
-vue.js等框架编写前端时,会比之前写jQuery更简单快捷

Python之Django rest_Framework框架认证源码分析,

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework.views import APIView
from rest_framework.response import Response

class TestView(APIView):

    def get(self, request, *args, **kwargs):
        # self.dispatch
        print(request.user)
        print(request.auth)
        return Response('GET请求,响应内容')

    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')

    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')

在下面刚开端请求进入,先不奉行本身写的章程,如:(get、post、put),它会先试行dispach方法,假若没有,就回到它的父类去找。

进行它父类的dispach

新蒲京官方下载 1

Django rest framework流程:

  原来用CBV的时候它延续**from django.views import
View中的views方法,在写get,post方法 如:class HostView(view)**

  要是有Django rest Framework它就无须承袭from django.views import
View中的views方法,而是:**

  先导入:**新蒲京官方下载 ,from rest_framework.views import
APIView它就能持续APIView,不在承袭django的views如:**class   
 AuthView(APIView):**

from rest_framework.views import APIView          先导入APIView 

from django.shortcuts import render,HttpResponse

class AuthView(APIView):                          继承APIView
    authentication_classes=[]
    def get(self,request):return Response('....’)

而在class
AuthView(APIView)中它继续的APIView,而
APIView它有继续了view,view它在rest_framework
view 中如下图:

新蒲京官方下载 2

点击view它就能够到django的view中,而以此view是此前CBV
中的那些view,如下图:

新蒲京官方下载 3

 

 

雄起雌伏的这几个类比原先django的那一个类跟多了点功能,而它的本质依然和原来的如出1辙先进行dispatch,本人有就施行本人的,未有就实行父类的,父类如若没有它就举行原来的dispatch 。

即便以前它的本来面目一样只是以后却产生变化,现在它的request是rest_framework中的request,而本来的request是django中的request,如:

class HostView(APIView):

    def get(self,request,*args,**kwargs):
        # 原来request对象,django.core.handlers.wsgi.WSGIRequest
        # 现在的request对象,rest_framework.request.Request\
        self.dispatch           请求进来先执行dispatch
        print(request.user)
        print(request.auth)
        return Response('主机列表')

一、请求进入先进行dispatch,而dispatch它在APIView中,dispatch在它里面有,先实践它和谐,假若把它先删除它就能够先进行暗许的不胜便是django提供,如下图:

新蒲京官方下载 4

 

呼吁进入它源码正是从后天起来如下:

源码的第三步:

 

    def dispatch(self, request, *args, **kwargs):    请求进来先执行dispatch
        """
        `.dispatch()` is pretty much the same as Django's regular dispatch,
        but with extra hooks for startup, finalize, and exception handling.
        """
        self.args = args                            dispatch先把参数获取到    
        self.kwargs = kwargs     
        #1、对request进行加工 
        '''
源码进来的第一步 1、request对象中(它比原来的类中封装了好多):

         request,
         parsers=self.get_parsers(),
         authenticators=self.get_authenticators(),    这里面放的是两个列的对象
         negotiator=self.get_content_negotiator(),
         parser_context=parser_context

        '''
        request = self.initialize_request(request, *args, **kwargs)      在这执行了这个方法,它返回了request
        self.request = request
        self.headers = self.default_response_headers  # deprecate?

        try:
第二步:2
            self.initial(request, *args, **kwargs)

            # Get the appropriate handler method
            if request.method.lower() in self.http_method_names:    
                 #在这执行反射
                handler = getattr(self, request.method.lower(),
                                  self.http_method_not_allowed)
            else: 
                #反射后拿到结果
                handler = self.http_method_not_allowed
第三步:3.执行get/post/delete/put等函数
            response = handler(request, *args, **kwargs)        返回结果 

        except Exception as exc:
            response = self.handle_exception(exc)
第四步:4、对返回结果再次加工
        self.response = self.finalize_response(request, response, *args, **kwargs)
        return self.response

 

在执行 request = self.initialize_request(request, *args,
**kwargs)时,先去协和写的找,未有再去父类找,下边是上下一心不曾就去父类找,如下图:

新蒲京官方下载 5

下边未有就去拿它的静态字段,如下图:

新蒲京官方下载 6

在这地方施行时request被加工,如下图:

新蒲京官方下载 7

 

源码实施的第一步:

    def initial(self, request, *args, **kwargs):
        """
        Runs anything that needs to occur prior to calling the method handler.
        """
        self.format_kwarg = self.get_format_suffix(**kwargs)

        # Perform content negotiation and store the accepted info on the request
        neg = self.perform_content_negotiation(request)
        request.accepted_renderer, request.accepted_media_type = neg

        # Determine the API version, if versioning is in use.
         2.1处理版本信息
        version, scheme = self.determine_version(request, *args, **kwargs)
        request.version, request.versioning_scheme = version, scheme

        # Ensure that the incoming request is permitted
          2.2认证授权
        self.perform_authentication(request)
          2.3权限验证
        self.check_permissions(request)
          2.4请求用户限制访问频率
        self.check_throttles(request)

推行源码第三步中的2.二评释授权:

 def initial(self, request, *args, **kwargs):
        """
        Runs anything that needs to occur prior to calling the method handler.
        """
        self.format_kwarg = self.get_format_suffix(**kwargs)

        # Perform content negotiation and store the accepted info on the request
        neg = self.perform_content_negotiation(request)
        request.accepted_renderer, request.accepted_media_type = neg

        # Determine the API version, if versioning is in use.
        version, scheme = self.determine_version(request, *args, **kwargs)
        request.version, request.versioning_scheme = version, scheme

        # Ensure that the incoming request is permitted
        self.perform_authentication(request)   执行这一步,点击就去如下图:
        self.check_permissions(request)  
        self.check_throttles(request)

还是先找自己的,没有才执行以下的:  
  def perform_authentication(self, request):
        """
        Perform authentication on the incoming request.

        Note that if you override this and simply 'pass', then authentication
        will instead be performed lazily, the first time either
        `request.user` or `request.auth` is accessed.
        """
        request.user    在这里request是传过来的request,不是原来的request

尽接着下一步它就能够找user,如下图:

新蒲京官方下载 8

找到user :

def user(self):
        """
        Returns the user associated with the current request, as authenticated
        by the authentication classes provided to the request.
        """
        if not hasattr(self, '_user'):   如果没有user
            self._authenticate()        就会这行self._authenticate()这个方法,在找到这个方法

return self._user

    def _authenticate(self):
        """
        Attempt to authenticate the request using each authentication instance
        in turn.
        """
       循环对象列表
        for authenticator in self.authenticators:  在这里authenticators是那个对象列表,它循环这个对象列表
            try:  
             执行每一个对象authenticate方法
                user_auth_tuple = authenticator.authenticate(self)     在这里它返回了两个值
            except exceptions.APIException:   没有验证成功就会报错
                self._not_authenticated()
                raise

            if user_auth_tuple is not None:
                self._authenticator = authenticator
                self.user, self.auth = user_auth_tuple      这里self.auth,self.user,就会有值,验证成功就会通过
                return

        self._not_authenticated()

 

 

新蒲京官方下载 9

 

rest_Framework框架认证源码分析,
# !/usr/bin/env python # -*- coding:utf-8 -*- from
rest_framework.views import APIView from rest_framework.response
import…

  1. django rest framework框架的作用?

    辅助开辟者能够神速支付出服从restful标准的API

  2. django rest framework框架都有何组件(拾)?

    -版本
    -权限
    -认证
    -节流
    -分页
    -解析器
    -连串化(最重视)
    -路由
    -视图
    -渲染器(把结果渲染到一个模板里)

四.什么是跨域?

  

-比如你访问百度,在百度js的ajax中携带一个京东的URL发送给你,由于浏览器的同源策略(同源是指域名,协议,端口相同),会进行阻隔,你接收不到页面内容

相关文章

No Comments, Be The First!
近期评论
    功能
    网站地图xml地图