首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >when i'm using python SDK for sent a post request?

when i'm using python SDK for sent a post request?

提问于 2019-08-01 17:08:25
回答 1关注 0查看 398

I want sent a post request

i have to use English ,becasue my RaspberryPi don't have pinyin

please use Cinese answer the question .

exception:

代码语言:js
复制
[TencentCloudSDKException] code:AuthFailure.SignatureFailure message:The provided credentials could not be validated because of exceeding request size limit, please use new signature method `TC3-HMAC-SHA256`. requestId:92c2a31c-4afe-4f34-a9e2-dc4fe2b35fc8

my code:

代码语言:python
复制
# helloworld/helloworld/view.py
from PIL import Image
import uuid
from django.http import HttpResponse
import json
from picamera import PiCamera
from time import sleep
import random
import requests
import datetime
import os
import base64
import sys
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException 
from tencentcloud.iai.v20180301 import iai_client, models 

def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024

def get_outfile(infile, outfile):
    if outfile:
        return outfile
    dir, suffix = os.path.splitext(infile)
    outfile = '{}-out{}'.format(dir, suffix)
    return outfile

def compress_image(infile, outfile='', mb=9000, step=10, quality=80):
    """不改变图片尺寸压缩到指定大小
    :param infile: 压缩源文件
    :param outfile: 压缩文件保存地址
    :param mb: 压缩目标,KB
    :param step: 每次调整的压缩比率
    :param quality: 初始压缩比率
    :return: 压缩文件地址,压缩文件大小
    """
    o_size = get_size(infile)
    if o_size <= mb:
        return infile
    outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im.save(outfile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(outfile)
    return outfile, get_size(outfile)

def createAnImage():
    camera = PiCamera()
    # camera.start_preview()
    camera.rotation = 180
    sleep(2)

    fileNameArry = []
    for i in range(0, 5):
        fileNameArry.append(random.randint(1, 50))
    fileName = "".join(str(word) for word in fileNameArry)
    longFileName = "/home/pi/Desktop/images/" + fileName + ".jpg"
    camera.capture(longFileName)
    compress_image(longFileName)
    # camera.stop_preview()
    return longFileName

def imageToBase64(langFileName):
    
    with open(langFileName, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        s = base64_data.decode()
        return s

def creatPerson(FileName,personName,personId):

    try:
        cred = credential.Credential("AKIDthNtIXWTczdCdxrJM9qLivxnRQRGFLbi", "5zIWZP3WouoPuENKLCrL5g6OVTGswuWL") 
        httpProfile = HttpProfile()
        httpProfile.endpoint = "iai.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = iai_client.IaiClient(cred, "ap-beijing", clientProfile)

        req = models.CreatePersonRequest()
        params = '{"GroupId":"group1","PersonName":"'+ personName +'","PersonId":"'+ personId +'","Image":"'+ imageToBase64(FileName) +'"}'
        req.from_json_string(params)

        resp = client.CreatePerson(req) 
        print(resp.to_json_string()) 

    except TencentCloudSDKException as err: 
        print(err) 


def addFamily(request):
    global return_json

    if request.is_ajax():

        data = json.loads(request.body.decode("utf8"))
        
        print(data)
        
        tencentReturnFlag = creatPerson()

        if BaiduReturnFlag == "0":
            return_json = {"msg": "添加成功"}
        else:
            return_json = {"msg": "添加失败"}
        
        os.remove(longFileName)

        return HttpResponse(json.dumps(return_json))
    
creatPerson(createAnImage(),"didi","201703204322")

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档