ModelArts windows使用pycurl进行推理

举报
星月菩提 发表于 2020/11/13 15:45:00 2020/11/13
【摘要】 ModelArts windows 推理


import pycurl
import json
import sys
import os
import certifi

class Storage:
    def __init__(self):
        self.contents = ''
        self.line = 0

    def store(self, buf):
        self.line = self.line + 1
        self.contents = "%s%i: %s" % (self.contents, self.line, buf)

    def __str__(self):
        return self.contents

retrieved_body = Storage()
retrieved_headers = Storage()


c = pycurl.Curl()
data = json.dumps({"auth": {"identity": {"methods": ["password"],"password": {"user": {"name": "xxxxx","password": "xxxxx","domain": {"name": "xxxxx"}}}},"scope": {"project": {"name": "cn-north-4"}}}})

c.setopt(c.URL,'https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens')
c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json;charset=utf8'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.setopt(c.WRITEFUNCTION, retrieved_body.store)
c.setopt(c.HEADERFUNCTION, retrieved_headers.store)
c.perform()
c.close()


#data = json.loads(retrieved_headers.contents)
#print(retrieved_headers.contents)
#print(retrieved_headers.contents.split("\\r\\n")[10])
token_value = "X-Auth-Token" + retrieved_headers.contents.split("\\r\\n")[10][22:]
print(token_value)
#print(data)

print("hello")

c = pycurl.Curl()
retrieved_body2 = Storage()
retrieved_headers2 = Storage()
c.setopt(pycurl.HTTPHEADER, [token_value])
c.setopt(c.URL,'https://3d18e25933ac4f96a8043f38839bdb73.apig.cn-north-4.huaweicloudapis.com/v1/infers/c769239f-2aa0-42ad-96fe-a78fbdae4fcf')
c.setopt(pycurl.POST, 1)
c.setopt(c.WRITEFUNCTION, retrieved_body2.store)
c.setopt(c.HEADERFUNCTION, retrieved_headers2.store)
c.setopt(c.HTTPPOST, [("images", (c.FORM_FILE, 'C:/Users/j00414895/Desktop/curl_info/cat2.jpg'))])
c.perform()
c.close()
print(retrieved_body2)

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。