通过阅读本文,您可以快速了解服务端Python SDK的使用方法。

前提条件

您已安装Python 2.7或以上版本。

操作步骤

  1. 登录服务端。
  2. 安装SDK。
    1. 安装阿里云核心SDK。
      sudo pip install aliyun-python-sdk-core
    2. 安装阿里云视频直播SDK。
      sudo pip install aliyun-python-sdk-live
  3. 更新SDK。
    1. 更新阿里云核心SDK。
      sudo pip install aliyun-python-sdk-core --upgrade
    2. 更新阿里云视频直播SDK。
      sudo pip install aliyun-python-sdk-live --upgrade
  4. 使用SDK。
    1. 文件说明。本文以SDK中的DescribeLiveStreamsFrameRateAndBitRateDataRequest.py文件为例介绍。
      # Licensed to the Apache Software Foundation (ASF) under one
      # or more contributor license agreements.  See the NOTICE file
      # distributed with this work for additional information
      # regarding copyright ownership.  The ASF licenses this file
      # to you under the Apache License, Version 2.0 (the
      # "License"); you may not use this file except in compliance
      # with the License.  You may obtain a copy of the License at
      #
      #     http://www.apache.org/licenses/LICENSE-2.0
      #
      #
      #
      # Unless required by applicable law or agreed to in writing,
      # software distributed under the License is distributed on an
      # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      # KIND, either express or implied.  See the License for the
      # specific language governing permissions and limitations
      # under the License.
      from aliyunsdkcore.request import RpcRequest
      class DescribeLiveStreamsFrameRateAndBitRateDataRequest(RpcRequest):
          def __init__(self):
              RpcRequest.__init__(self, 'live', '2016-11-01', 'DescribeLiveStreamsFrameRateAndBitRateData','live')
          def get_SecurityToken(self):
              return self.get_query_params().get('SecurityToken')
          def set_SecurityToken(self,SecurityToken):
              self.add_query_param('SecurityToken',SecurityToken)
          def get_OwnerId(self):
              return self.get_query_params().get('OwnerId')
          def set_OwnerId(self,OwnerId):
              self.add_query_param('OwnerId',OwnerId)
          def get_DomainName(self):
              return self.get_query_params().get('DomainName')
          def set_DomainName(self,DomainName):
              self.add_query_param('DomainName',DomainName)
          def get_AppName(self):
              return self.get_query_params().get('AppName')
          def set_AppName(self,AppName):
              self.add_query_param('AppName',AppName)
          def get_StreamName(self):
              return self.get_query_params().get('StreamName')
          def set_StreamName(self,StreamName):
              self.add_query_param('StreamName',StreamName)
          def get_StartTime(self):
              return self.get_query_params().get('StartTime')
          def set_StartTime(self,StartTime):
              self.add_query_param('StartTime',StartTime)
          def get_EndTime(self):
              return self.get_query_params().get('EndTime')
          def set_EndTime(self,EndTime):
              self.add_query_param('EndTime',EndTime)

      该文件中对于DescribeLiveStreamsFrameRateAndBitRateDataRequest这个类,有若干组一一对应的get_Xset_X方法,分别用于获取和设定该API请求的参数。

    2. 调用接口。此处以v20161101版本为例,调用DescribeLiveStreamsFrameRateAndBitRateDataRequest接口。
      #引入aliyunsdkcore包
      from aliyunsdkcore import client
      #引入aliyunsdklie包
      from aliyunsdklive.request.v20161101 import DescribeLiveStreamsFrameRateAndBitRateDataRequest
      #初始化client
      #AK和Secrect需要自己提供(可以在阿里云控制台中找到)
      #以访问cn-hangzhou region为例
      clt = client.AcsClient(AK, Secret, 'cn-hangzhou')
      #构造需发起的API请求调用
      request = DescribeLiveStreamsFrameRateAndBitRateDataRequest.DescribeLiveStreamsFrameRateAndBitRateDataRequest()
      request.set_DomainName('qt1.alivecdn.com')
      request.set_AppName('caster')
      request.set_StreamName('cc8d5e157f5d4146afd091e246efd1f6')
      #发起请求
      result = clt.do_action_with_exception(request)
      #请求结果
      #result为请求结果的ResponseBody字符串
      print result
      #如果您需要进一步对result进行处理,可以根据每个接口的返回格式,进行格式化处理并进一步分析或使用
      #对于DescribeLiveStreamsFrameRateAndBitRateDataRequest来说,它返回一个JSON格式的数据,我们可以用json包来处理这些response
      import json
      x = json.loads(result)
      print x['RequestId']
      print x['FrameRateAndBitRateInfos']

后续步骤

如果您需要移除SDK,请执行以下命令:
  • sudo pip uninstall aliyun-python-sdk-core
  • sudo pip uninstall aliyun-python-sdk-live