调用API


代码包含

  • use-api.py
  • post.py
  • get-token.py

use-api.py

import post

name = " " 
password = " "
api_url = " " # api地址
image_path = r"timg.jpg"  # 图像文件完整路径

# 发送请求
r = post.main(name, password, api_url, image_path)
print(r)

post.py

import requests
import os
import get_token


def main(name, password, api_url, image_path):
    image_name = image_path.split('\\')[-1]
    # 查看本地是否存在token
    if os.path.exists("token.txt"):
        with open("token.txt", 'r') as f:
            token = f.read()
    else:
        # 调用get_token.py中的函数获取token
        token = get_token.main(name, password)
        # 缓存token以便下次使用
        with open("token.txt", 'w') as f:
            f.write(token)
    if token == '':
        return "token获取错误"
    else:
        header = {
                "X-Auth-Token": token,
                }
        files = {
                "images": (image_name, open(image_path,'rb'),'image/png')
                }
        z = requests.post(
                    api_url,
                    headers = header,
                    files=files,
                    verify=False,
                    )
        if z.status_code == 200:
            return z.text
        elif len(z.text) != 0:
            return z.text
        else:
            return "程序错误"
    
    
if __name__ == '__main__':
    name = " "         # 用户名
    password = " " # 密码
    api_url = " " # api地址
    image_path = r"test.jpg"  # 图像文件完整路径
    r = main(name, password, api_url, image_path)
    print(r)

get-token.py

import requests

def main(name, password, domain_name="", region="cn-north-4"):
    if domain_name == "":
        domain_name = name
    urls = "https://iam." + region + ".myhuaweicloud.com/v3/auth/tokens"
    header = {
        "User-Agent": "PostmanRuntime/7.24.1",
        "Content-Type": "application/json;charset=utf8",
    }
    json_data = {
        "auth": {
            "identity": {
                "methods": ["password"],
                "password": {
                    "user": {
                        "name": name,
                        "password": password,
                        "domain": {
                            "name": domain_name,
                        }
                    }
                }
            },
            "scope": {
                "project": {
                    "name": region
                }
            }
        }
    }
    z = requests.post(
                    url = urls,
                    headers = header,
                    json = json_data,
                    verify = False,
                    )
    
    if z.status_code == 201:
        return z.headers["X-Subject-Token"]
    elif z.status_code == 401:
        print(z.text)
        return ''
    else:
        print(z.status_code)
        return ''


if __name__ == '__main__':
    name = ' '         # 用户名
    password = ' '     # 密码
#    domain_name = ' '  # 可选,所属账号,默认为用户名
#    region="cn-north-4"     # 可选,使用区域
    token = main(name, password)
    if token != '':
        print(token)

小结

  1. 将name,password,api_url更改,即可进行API的调用。
  2. 一段代码:
    z=request.post(api_url,headers=header,files=files,verify=False)
    if z.status_code ==200:
       return z.text
    
  3. 比较难理解的是那个get-token,查阅Modelarts文档的json格式。
    再利用requests进行post

文章作者: ╯晓~
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ╯晓~ !
评论
  目录