python上传视频

2017-01-01 10:46
  • http简单封装
class ConfigHttp:
    def __init__(self, host, port,headers):
        self.host = host
        self.port = port
        self.headers = headers
    # 封装HTTP GET请求方法
    def get(self, url, params=None):
        # params = urllib.parse.urlencode(params)
        url = "http://"+self.host+":"+self.port+url
        print(url)
        try:
            r = requests.get(url, params=params, headers=self.headers)
            r.encoding = 'UTF-8'
            dcit_r = json.loads(r.text)
            print(json.loads(r.text))
            return dcit_r
        except Exception:
            print('no json data returned')
            return {}
    # 封装HTTP POST请求方法,支持上传图片
    def post(self, url, data=None, files=None):
        # data = eval(data)
        url = 'http://' + self.host + ':' + str(self.port)+url
        r =requests.post(url, files=files, data=data)
        json_response = r.text
        print(json_response)
        return json_response
  • 上传视频
def upload():
    getToken()
    f = {'Filename':'视频上传测试1',
             'Filedata':('1.mp4',open(u'D:/app/dgm/1.mp4','rb'),'application/octet-stream'),
             'Upload':'视频上传测试2'
    }
    up = baseHttp.ConfigHttp(host=UPLOAD_HOST,port=UPLOAD_PORT,headers=HEADER)
    url = "/api/upload?do=upload&type=4&op=video&sid="+CODE +"&token=" + TOKEN
    res = up.post(url=url, files=f)