码迷,mamicode.com
首页 > 其他好文 > 详细

test

时间:2018-05-27 16:24:53      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:head   cep   参数   file   urllib   time()   open   imp   ide   

# -*- coding: utf-8 -*-
import urllib2,json
import urllib
import time

http_url=https://api-cn.faceplusplus.com/facepp/v3/compare
key = "iMP75CfbxCNQW4_DQkuEyW1F7ACFAZuN"
secret = "DI0ORfbskcQLs0hRhyLN60OXo_7WYoP8"

def getFaceInfo(file1path,file2path):
    # boundary是 :---------本地运行时时间
    boundary = ----------%s % hex(int(time.time() * 1000))

    data = []
    data.append(--%s % boundary)
    data.append(Content-Disposition: form-data; name="%s"\r\n % api_key)
    data.append(key)
    data.append(--%s % boundary)
    data.append(Content-Disposition: form-data; name="%s"\r\n % api_secret)
    data.append(secret)

    # 请求参数,图片的添加
    data.append(--%s % boundary)
    fr1 = open(file1path, rb)
    data.append(Content-Disposition: form-data; name="%s"; filename=" " % image_file1)
    data.append(Content-Type: %s\r\n % application/octet-stream)
    data.append(fr1.read())
    fr1.close()

    data.append(--%s % boundary)
    fr2 = open(file2path, rb)
    data.append(Content-Disposition: form-data; name="%s"; filename=" " % image_file2)
    data.append(Content-Type: %s\r\n % application/octet-stream)
    data.append(fr2.read())
    fr2.close()
    data.append(--%s--\r\n % boundary)

    http_body = \r\n.join(data)
    # buld http request
    req = urllib2.Request(http_url)
    # header
    req.add_header(Content-Type, multipart/form-data; boundary=%s % boundary)
    req.add_data(http_body)
    try:
        # req.add_header(‘Referer‘,‘http://remotserver.com/‘)
        # post data to server
        resp = urllib2.urlopen(req, timeout=5)
        # get response
        qrcont = resp.read()
        # print(qrcont)
        info = json.loads(qrcont)
        confidence = info[confidence]
        print(confidence)

    except urllib2.HTTPError as e:
        print(e.read())

file1path = r"D:\3.jpg"
file2path = r"D:\4.jpg"
getFaceInfo(file1path,file2path)

 

test

标签:head   cep   参数   file   urllib   time()   open   imp   ide   

原文地址:https://www.cnblogs.com/lulipro/p/9096256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!