def uploadData(self, localPath, uploadName): print 'uploading data' req = request.Request(host=self.__host, protocol=constant.HTTP, url='/credential/upload/data/' + uploadName, method="GET", time_out=3000) a, b, rr = self.cli.execute(req) try: res = json.loads(rr) except: print a, b, rr return if res['success'] != True: print res return token = res['credential'] auth = oss2.StsAuth(token['AccessKeyId'], token['AccessKeySecret'], token['SecurityToken']) bucket = oss2.Bucket(auth, self.__endpoint, self.__bucket) oss2.resumable_upload(bucket, res['path'], localPath) print 'upload complete'
def predict(url, app_key, app_secret, request_data): cli = client.DefaultClient(app_key=app_key, app_secret=app_secret) body = request_data url_ele = urlparse(url) host = 'http://' + url_ele.hostname path = url_ele.path req_post = request.Request(host=host, protocol=constant.HTTP, url=path, method="POST", time_out=6000) req_post.set_body(body) req_post.set_content_type(constant.CONTENT_TYPE_STREAM) stat,header, content = cli.execute(req_post) return stat, dict(header) if header is not None else {}, content
def post(self): print "send data" cli = client.DefaultClient(self.__appcode) req = request.Request(host=self.__host, protocol=constant.HTTP, url=self.__url, method="POST", time_out=self.__time_out) req.set_body(self.__body) # for k,v in self.__body.items(): # print "key:"+k+",value:"+str(v) req.set_content_type(constant.CONTENT_TYPE_FORM) res = cli.execute(req) self.__resHead = res[1] json_res = json.loads(res[2]) return json_res
def get(self): self.__url = self.__url + '?' for k, v in self.__body.items(): self.__url = self.__url + k + "=" + str(v) + "&" self.__url = self.__url[0:len(self.__url) - 1] print "send data" cli = client.DefaultClient(self.__appcode) req = request.Request(host=self.__host, protocol=constant.HTTP, url=self.__url, method="GET", time_out=self.__time_out) res = cli.execute(req) self.__resHead = res[1] json_res = json.loads(res[2]) return json_res
def new_request(self, **kwargs): """ Create a new request with some parameters :param kwargs: :return: `RequestInitializer` """ url = self.config["base_url"] if kwargs.get("user_id") is not None: url = url + kwargs["user_id"] self.req = request.Request(host=self.config["host"], protocol=constant.HTTP, url=url, method=kwargs["method"], time_out=kwargs["timeout"]) return self
def predict(url, app_key, app_secret, img_base64, kv_config, old_format): statTime = time.time() cli = client.DefaultClient(app_key=app_key, app_secret=app_secret) if not old_format: param = {} param['image'] = img_base64 if kv_config is not None: param['configure'] = json.dumps(kv_config) body = json.dumps(param) else: param = {} pic = {} pic['dataType'] = 50 pic['dataValue'] = img_base64 param['image'] = pic if kv_config is not None: conf = {} conf['dataType'] = 50 conf['dataValue'] = json.dumps(kv_config) param['configure'] = conf inputs = {"inputs": [param]} body = json.dumps(inputs) url_ele = urlparse(url) host = 'http://' + url_ele.hostname path = url_ele.path headers = {} req_post = request.Request(host=host, protocol=constant.HTTP, url=path, headers=headers, method="POST", time_out=6000) req_post.set_body(bytearray(source=body, encoding="utf8")) req_post.set_content_type(constant.CONTENT_TYPE_STREAM) stat, header, content = cli.execute(req_post) endTime = time.time() return stat, dict(header) if header is not None else {}, content
PAGENUM = "1" if not DATE: DATE = datestamp # 设定存放iocs的csv文件名及相对路径 IOCS_CSVNAME = "archive/IOCS_" + DATE + ".csv" # 设置ALI云API请求参数 host = "https://apiiocs.sec-un.com" url = "/v1/iocs" cli = client.DefaultClient(app_key=APPKEY, app_secret=APPSECRET) req_post = request.Request(host=host, protocol=constant.HTTPS, url=url, method="POST", time_out=120) bodyMap = {} def main(): global PAGENUM bodyMap["token"] = TOKEN bodyMap["date"] = DATE bodyMap["page"] = PAGENUM # bodyMap["type"] = "indicator" # bodyMap["qurey"] = "reports" req_post.set_body(bodyMap)
# -*- coding: utf-8 -*- from com.aliyun.api.gateway.sdk import client from com.aliyun.api.gateway.sdk.http import request from com.aliyun.api.gateway.sdk.common import constant host = "http://787f97ec61ed4e469c38b7dc72ebf82f-cn-beijing.alicloudapi.com" url = "/user?first_pay_year=2019&partner_code=ahhdq&product_cd=59" # url = "/user" cli = client.DefaultClient(app_key="app_key", app_secret="app_secret") # GET req = request.Request(host=host, protocol=constant.HTTP, url=url, method="GET", time_out=30000) print(cli.execute(req)) #post body stream # import json # req_post = request.Request(host=host, protocol=constant.HTTP, url=url, method="POST", time_out=30000) # body = {} # body["first_pay_year"] = "2019" # body["partner_code"] = "ahhdq" # body["product_cd"] = "59" # print(json.dumps(body)) # req_post.set_body(body) # req_post.set_content_type(constant.CONTENT_TYPE_STREAM) # print(cli.execute(req_post))
#!/usr/bin/env python #encoding=utf-8 from com.aliyun.api.gateway.sdk import client from com.aliyun.api.gateway.sdk.http import request from com.aliyun.api.gateway.sdk.common import constant host = "http://sms.market.alicloudapi.com" url = "/singleSendSms" \ + '?ParamString={"no":"123456"}' \ + "&RecNum=13120713129" \ + "&SignName=勤工巧匠" \ + "&TemplateCode=SMS_78625127" req = request.Request(host=host, url=url, method="GET", time_out=30000) cli = client.DefaultClient(app_key="3a46b15289a3475faaccd05be1506aa5", app_secret="APP_SECRET") print cli.execute(req)
def execute(self, code, params): print "creating job" req_post = request.Request(host=self.__host, protocol=constant.HTTP, url="/job", method="POST", time_out=30000) req_post.set_body(bytearray(source=json.dumps({"code": code, "params": pickle.dumps(params)}), encoding="utf8")) req_post.set_content_type(constant.CONTENT_TYPE_STREAM) a, b, res = self.cli.execute(req_post) # debug print res try: r = json.loads(res) except: print a, b, res return if r['success'] != True: print r return print 'jobId is ' + r['jobId'] token = r['credential'] auth = oss2.StsAuth(token['AccessKeyId'], token['AccessKeySecret'], token['SecurityToken']) bucket = oss2.Bucket(auth, self.__endpoint, self.__bucket) nextLogPosition = 0 while 1: req = request.Request(host=self.__host, protocol=constant.HTTP, url='/result/' + r['jobId'], method="GET", time_out=3000) a, b, rr = self.cli.execute(req) try: jsonObj = json.loads(rr) except: print 'get result error:', a, b, rr continue if jsonObj['success'] != True: print jsonObj return print jsonObj['status'] try: log_desc = bucket.head_object(r['logPath']) l = log_desc.content_length - 1 if l > nextLogPosition: log_stream = bucket.get_object(r['logPath'], byte_range=(nextLogPosition, l)) log = log_stream.read() sys.stdout.write(log) sys.stdout.flush() with open('logs/job' + r['jobId'] + '.log', 'a') as the_file: the_file.write(log) nextLogPosition = l except: pass if jsonObj['status'] == 'finished': try: result_desc = bucket.head_object(r['resultPath']) print 'result length is ' + str(result_desc.content_length) result_stream = bucket.get_object(r['resultPath']) return r['jobId'], pickle.loads(result_stream.read()) except: raise pass time.sleep(1)
# -*- coding: utf-8 -*- import json import uuid from com.aliyun.api.gateway.sdk import client from com.aliyun.api.gateway.sdk.http import request from com.aliyun.api.gateway.sdk.common import constant host = "${HOST}" url = "${URL}" cli = client.DefaultClient(app_key="${APP_KEY}", app_secret="${APP_SECRET}") req_post = request.Request(host=host, protocol=constant.HTTPS, url=url, method="POST", time_out=30000, sign_with_body=False) bodyMap = { 'id': str(uuid.uuid4()), 'version': "1.0", 'request': { 'iotToken': "xxxx", 'apiVer': "1.0.0" }, 'params': { #接口参数 } } req_post.set_body(bodyMap) req_post.set_content_type(constant.CONTENT_TYPE_JSON)