ModelArts windows使用pycurl进行推理
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)
- 点赞
- 收藏
- 关注作者
评论(0)