ModelArts windows使用pycurl进行推理
【摘要】 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)