标签: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)
标签:head cep 参数 file urllib time() open imp ide
原文地址:https://www.cnblogs.com/lulipro/p/9096256.html