亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python django框架 web端視頻加密的實(shí)例詳解

 更新時(shí)間:2020年11月20日 09:55:28   作者:於 兔シ  
這篇文章主要介紹了Python django框架 web端視頻加密,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

視頻加密流程圖:

視頻加密流程圖

后端獲取保利威的視頻播放授權(quán)token,提供接口api給前端

參考文檔:http://dev.polyv.net/2019/videoproduct/v-api/v-api-play/create-playsafe-token/

在utils下創(chuàng)建polyv.py,編寫token生成工具函數(shù),path:utils/polyv.py
utils 是多個(gè)模塊的公共函數(shù)庫(kù)的文件夾里面存放自己開發(fā)的組件

from django.conf import settings
import time
import requests
#pip install requests
import hashlib



class PolyvPlayer(object):
  def __init__(self,userId,secretkey,tokenUrl):
    """初始化,提供用戶id和秘鑰"""
    self.userId = userId
    self.secretKey = secretkey
    self.tokenUrl = tokenUrl


  def tomd5(self, value):
    """取md5值"""
    return hashlib.md5(value.encode()).hexdigest()

  # 獲取視頻數(shù)據(jù)的token
  def get_video_token(self, videoId, viewerIp, viewerId=None, viewerName='', extraParams='HTML5'):
    """
    :param videoId: 視頻id
    :param viewerId: 看視頻用戶id
    :param viewerIp: 看視頻用戶ip
    :param viewerName: 看視頻用戶昵稱
    :param extraParams: 擴(kuò)展參數(shù)
    :param sign: 加密的sign
    :return: 返回點(diǎn)播的視頻的token
    """
    ts = int(time.time() * 1000) # 時(shí)間戳
    plain = {
      "userId": self.userId,
      'videoId': videoId,
      'ts': ts,
      'viewerId': viewerId,
      'viewerIp': viewerIp,
      'viewerName': viewerName,

    }

    # 按照ASCKII升序 key + value + key + value... + value 拼接
    plain_sorted = {}
    key_temp = sorted(plain)
    for key in key_temp:
      plain_sorted[key] = plain[key]

    plain_string = ''
    for k, v in plain_sorted.items():
      plain_string += str(k) + str(v)

    # 首尾拼接上秘鑰
    sign_data = self.secretKey + plain_string + self.secretKey

    # 取sign_data的md5的大寫
    sign = self.tomd5(sign_data).upper()

    # 新的帶有sign的字典
    plain.update({'sign': sign})
    # python 提供的發(fā)送http請(qǐng)求的模塊
    result = requests.post(

      url=self.tokenUrl,
      headers={"Content-type": "application/x-www-form-urlencoded"},
      data=plain     # 平臺(tái)所需要攜帶的數(shù)據(jù)
    ).json() # json.loads 把那拿到的數(shù)據(jù)序列化
    token = {} if isinstance(result, str) else result.get("data", {})  # 如果保利威視頻平臺(tái)返回的的字符串 token={} 否則
    if token == '':
      return result
    return token

在 項(xiàng)目開發(fā)時(shí)的本地配置 配置參數(shù):
配置文件settings/dev.py,代碼:

# 保利威視頻加密服務(wù)
POLYV_CONFIG = {
  "userId":"62dc475e3f",
  "secretkey":"h6FiaEBRMU",
  "tokenUrl":"https://hls.videocc.net/service/v1/token",
}

保利威文檔地址:https://my.polyv.net/secure/setting/api在這里插入圖片描述
保利威api參考文檔:http://dev.polyv.net/2019/videoproduct/v-api/v-api-play/create-playsafe-token/

在這里插入圖片描述

urls.py,主路由代碼:

path(r'polyv/',include('polyv.urls')),

在項(xiàng)目主應(yīng)用文件夾下創(chuàng)建app
命令:
cd 主應(yīng)用文件夾下
python3 …/…/manage.py startapp polyv

urls.py,子路由代碼:

from django.urls import path,re_path
from . import views

urlpatterns = [
  path('video/',views.Video.as_view(),)

]

polyv/views.py,視圖代碼:

from django.shortcuts import render

# Create your views here.
from rest_framework import status
from lyapi.utils.polyv import PolyvPlayer
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from django.conf import settings
from rest_framework.response import Response


class VideoView(APIView):
  # vid = '348e998797383060cb19620b1c600203_3'
  # permission_classes = [IsAuthenticated, ]       #from rest_framework.permissions import IsAuthenticated 登錄認(rèn)證
  def get(self,request):
    polyv_obj = PolyvPlayer(settings.POLYV_CONF['userid'],settings.POLYV_CONF['secretKey'],settings.POLYV_CONF['tokenUrl'])   # 調(diào)用polyv文件下的polyv_obj類
    # vid = 'cee1047a76927eb43774263cd93bb69f_c'    # 存在保利威平臺(tái)的視頻ID
    # vid = '348e998797383060cb19620b1c600203_3'     # 存在保利威平臺(tái)的視頻ID
    vid = request.query_params.get('vid')				# 需要把保利威平臺(tái)的視頻ID存在數(shù)據(jù)庫(kù)里或者直接或者在前段直接在的Params加 vid cee1047a76927eb43774263cd93bb69f_c
    viewerIp = request.META.get('REMOTE_ADDR')     # 獲取用戶訪問(wèn)的IP地址
    viewerId = request.user.id             # 獲取用戶的id
    viewerName = request.user.username         # 獲取用戶的賬號(hào)名

    token_dict = polyv_obj.get_video_token(vid,viewerIp,viewerId,viewerName)    # 調(diào)用polyv文件下的polyv_obj類下的get_video_token方法
    print(token_dict)

    if 'code' in list(token_dict) and token_dict['code'] != 200:            # 返回請(qǐng)求失敗的信息
      return Response(token_dict, status=status.HTTP_403_FORBIDDEN)
    '''
    返回結(jié)果:
    {
      "code": 403,
      "status": "error",
      "message": "invalid userId or videoId.",
      "data": ""
    }
    
    '''

    return Response(token_dict)             # 返回請(qǐng)求成功的信息

    '''
    返回結(jié)果:
      {
        "token": "43883858-92a3-4f25-a6e8-701d10d88cde-f2",
        "userId": "cee1047a76",
        "appId": null,
        "videoId": "cee1047a76927eb43774263cd93bb69f_c",
        "viewerIp": "127.0.0.1",
        "viewerId": "2",
        "viewerName": "root",
        "extraParams": null,
        "ttl": 600000,
        "createdTime": 1605614888570,
        "expiredTime": 1605615488570,
        "iswxa": 0,
        "disposable": false
      }
    '''

到這里后端的api接口就已經(jīng)寫好啦!
前段 vue界面 簡(jiǎn)寫:
在src下的components下新建一個(gè)Player.vue

# Player.vue

<template>
  <div class="player">
   <div id="player">

   </div>
  </div>
</template>

<script>
export default {
 name:"Player",
 data () {
  return {

  }
 },


 mounted() { //如果需要對(duì)標(biāo)簽進(jìn)行一些加工處理,然后再放數(shù)據(jù)時(shí),需要用mounted這個(gè)鉤子函數(shù),如果單純的是獲取數(shù)據(jù),添加到數(shù)據(jù)屬性中
        // 那么用created方法

  this.get_video_data();

 },

 methods: {
    get_video_data(){
     let user_name = localStorage.username || sessionStorage.username; //token認(rèn)證
    let token = localStorage.token || sessionStorage.token;				//token認(rèn)證
    console.log(this.$route.params.vid)
     let self = this;
    var player = polyvPlayer({
     wrap: '#player',
     width: document.documentElement.clientWidth - 300,
     height: document.documentElement.clientHeight,
     vid: this.$route.params.vid,
     // forceH5: true,

     // code: user_name,
     playsafe: (vid, next) =>{
      console.log(self)
      self.$axios.get(`${self.$settings.Host}/polyv/video/?vid=${self.$route.params.vid}`,{
       headers:{
       'Authorization':'jwt ' + token
       }
      }).then((res)=>{
       // {‘token':'asasfd'}
       next(res.data.token);

      }).catch((error)=>{

      })


     }
    });
   }
 },
 computed: {
 }
}
</script>

<style scoped>
</style>

src下的router的index.js配置url:

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Login from '@/components/Login'
import Register from "@/components/Register";
import Course from "@/components/Course"
import Detail from "@/components/Detail";
import Cart from "@/components/Cart";
import Order from "@/components/Order";
import Player from "@/components/Player";	//Player組件的url 需要復(fù)制這里,其他的url忽略
import Myorder from "@/components/Myorder";	 
Vue.use(Router)

export default new Router({
 mode:'history',
 routes: [
  {
   path: '/',
   //name: 'heme',
   component: Home
  },
    {
   path: '/home',
   //name: 'heme',
   component: Home
  },
    {
   path: '/user/login',
   //name: 'heme',
   component: Login
  },
    {
   path: '/register',
   //name: 'heme',
   component: Register
  },
    {
   path: '/courses',
   //name: 'heme',
   component: Course

  },
  {
   path: '/courses/detail/:id',
   //name: 'heme',
   component: Detail
  },
  {
   path: '/cart',
   //name: 'heme',
   component: Cart
  },
  {
   path: '/order',
   //name: 'heme',
   component: Order
  },
  {
   path: '/myorder',
   //name: 'heme',
   component: Myorder
  },
  {//Player組件的url 需要復(fù)制這個(gè)括號(hào)里的,其他的url忽略
   path: '/polyv/player/:vid',
   //name: 'heme',
   component: Player
  },

 ]
})

自己配置的 訪問(wèn)路徑http://www.luffycity.cn:8080/polyv/player/cee1047a76927eb43774263cd93bb69f_c```

到此這篇關(guān)于Python django框架 web端視頻加密的文章就介紹到這了,更多相關(guān)Python django框架視頻加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python跑循環(huán)時(shí)內(nèi)存泄露的解決方法

    Python跑循環(huán)時(shí)內(nèi)存泄露的解決方法

    這篇文章主要介紹了Python跑循環(huán)時(shí)內(nèi)存泄露的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Django單元測(cè)試的具體使用

    Django單元測(cè)試的具體使用

    Django提供了一套強(qiáng)大的測(cè)試工具來(lái)幫助開發(fā)者編寫和運(yùn)行單元測(cè)試,本文就來(lái)介紹一下Django中的單元測(cè)試,包括測(cè)試原理、編寫測(cè)試用例和運(yùn)行測(cè)試,感興趣的可以了解一下
    2023-11-11
  • Python?網(wǎng)頁(yè)請(qǐng)求之requests庫(kù)的使用詳解

    Python?網(wǎng)頁(yè)請(qǐng)求之requests庫(kù)的使用詳解

    requests?是?Python?中比較常用的網(wǎng)頁(yè)請(qǐng)求庫(kù),主要用來(lái)發(fā)送?HTTP?請(qǐng)求,在使用爬蟲或測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到,使用起來(lái)十分簡(jiǎn)潔,這篇文章主要介紹了Python?網(wǎng)頁(yè)請(qǐng)求之requests庫(kù)的使用詳解,需要的朋友可以參考下
    2022-09-09
  • Python smallseg分詞用法實(shí)例分析

    Python smallseg分詞用法實(shí)例分析

    這篇文章主要介紹了Python smallseg分詞用法,以實(shí)例形式分析了Python實(shí)現(xiàn)分析的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Pytest參數(shù)化parametrize使用代碼實(shí)例

    Pytest參數(shù)化parametrize使用代碼實(shí)例

    這篇文章主要介紹了Pytest參數(shù)化parametrize使用代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Python AI編程助手AICodeHelper使用示例

    Python AI編程助手AICodeHelper使用示例

    這篇文章主要介紹了Python AI編程助手AICodeHelper使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-12-12
  • Python正則表達(dá)式經(jīng)典入門教程

    Python正則表達(dá)式經(jīng)典入門教程

    這篇文章主要介紹了Python正則表達(dá)式,結(jié)合具體實(shí)例形式歸納總結(jié)了Python正則表達(dá)式中常用的各種函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下
    2017-05-05
  • python 3.6.4 安裝配置方法圖文教程

    python 3.6.4 安裝配置方法圖文教程

    這篇文章主要為大家詳細(xì)介紹了python 3.6.4 安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Python中 Global和Nonlocal的用法詳解

    Python中 Global和Nonlocal的用法詳解

    global關(guān)鍵字用來(lái)在函數(shù)或其他局部作用域中使用全局變量, nonlocal聲明的變量不是局部變量,也不是全局變量,而是外部嵌套函數(shù)內(nèi)的變量。這篇文章主要介紹了Python中 Global和Nonlocal的用法,需要的朋友可以參考下
    2020-01-01
  • Python中Pickling和Unpickling的區(qū)別詳解

    Python中Pickling和Unpickling的區(qū)別詳解

    在本文中,我們將探討 Python 中 pickling 和 unpickling 之間的主要區(qū)別,我們將詳細(xì)討論 Python pickling 和 unpickling 的概念,包括它們的目的、語(yǔ)法、用法以及安全可靠的 pickling 和 unpickling 操作的注意事項(xiàng),需要的朋友可以參考下
    2023-09-09

最新評(píng)論