1 查询List

参考自:python2版本


#!/usr/bin/env python#-*- coding:utf-8 -*-
#import urllib2
import urllib
import hashlib
import hmac
import base64
import json
import requestsdef requestUrl(url, apike, signature, command):request = {}request['command'] = commandrequest['apikey'] = apikeyrequest['response'] = 'json'secretkey = signature#request_url = '&'.join(['='.join([r,urllib.parse.quote_plus(request[r])] for r in request.keys())])request_url = '&'.join(['='.join([r, urllib.parse.quote_plus(request[r])]) for r in request.keys()])#sig_url = '&'.join(['='.join([r.lower(), urllib.parse.quote_plus(request[r]).lower()] for r in sorted(request.iterkeys())) ])sig_url = '&'.join(['='.join([r.lower(), urllib.parse.quote_plus(request[r]).lower()]) for r in sorted(request.keys())])sig = urllib.parse.quote_plus(base64.encodebytes(hmac.new(secretkey.encode('utf8'), sig_url.encode('utf8'), hashlib.sha1).digest()).strip())req = url +'?' +request_url + '&signature=' + sigreturn reqdef reqresults(request_url):# res = urllib.request.urlopen(request_url)r = requests.get(request_url)print(r.status_code)print(r.content.decode())# return res.read()if __name__ == '__main__':url = 'http://localhost:8080/client/api'apikey = 'xxxxxxxxxxxxx'command = 'listZones'  # 命令行,这个根据自己索取内容填写secretkey = 'yyyyyyyyyyyyy'request_url = requestUrl(url, apikey, secretkey, command)print(request_url)reqresults(request_url)#  resultsNeed = json.loads(reqresults(request_url))

主要作用是生成访问API接口的URL命令。返回结果如下:

http://localhost:8080/client/api?command=listZones&apikey=fXiib65QMDqStzPerUa0tDOaQ5Zl0GTozBUuLwUKPv5sn7DDhvQ3DRLkteR8eN_BD-HVURJDwnJhiKXgJ9tGTg&response=json&signature=4%2F6xT4D%2BAe4Eb%2Fsq%2F74il0YgCXk%3D
200
{"listzonesresponse":{"count":1,"zone":[{"id":"e654be98-a73f-42e9-afaf-248ae6e2ea29","name":"Sandbox-simulator","dns1":"10.147.28.6","internaldns1":"10.147.28.6","guestcidraddress":"10.1.1.0/24","networktype":"Advanced","securitygroupsenabled":false,"allocationstate":"Enabled","zonetoken":"522ff2e7-48d6-33ff-89a5-7908e3ed2921","dhcpprovider":"VirtualRouter","localstorageenabled":false,"tags":[]}]}}

2 command命令

参考自 Python2版本

#!/usr/bin/env python
import urllib
import hashlib
import hmac
import base64
import json
import requestsdef reqresults(request_url):# res = urllib.request.urlopen(request_url)r = requests.get(request_url)print(r.status_code)print(r.content.decode())# return res.read()baseurl='http://localhost:8080/client/api'#这个字典存放命令下的参数,这里是创建计算方案'command':'createServiceOffering',
request={
'name':'json4c8g',
'command':'createServiceOffering',
'storagetype':'shared',
'memory':'8096',
'cpunumber':'4',
'displaytext':'asdasd',
'cpuspeed':'1800',
'domainid':'1',
}#下面的这样写字典的方式主要是和上面有个区分,这里是必须写的参数
request['response']='json'
request['apikey']='fXiib65QMDqStzPerUa0tDOaQ5Zl0GTozBUuLwUKPv5sn7DDhvQ3DRLkteR8eN_BD-HVURJDwnJhiKXgJ9tGTg'
secretkey='p0o4Yga9nnIpOs4-xZ-onjGwJe5_NTAJbXpu7xwjXlD202wsFpX8NdFIITPJN_uBAj3Scho_6FDPlkFWmvKXRg'
request_url='&'.join(['='.join([r, urllib.parse.quote_plus(request[r])]) for r in request.keys()])
sig_url = '&'.join(['='.join([r.lower(), urllib.parse.quote_plus(request[r]).lower()]) for r in sorted(request.keys())])
sig = urllib.parse.quote_plus(base64.encodebytes(hmac.new(secretkey.encode('utf8'), sig_url.encode('utf8'), hashlib.sha1).digest()).strip())
req = baseurl + '?' + request_url + '&signature=' + sigprint(req)
res= reqresults(req)

结果:

http://localhost:8080/client/api?name=json4c8g&command=createServiceOffering&storagetype=shared&memory=8096&cpunumber=4&displaytext=asdasd&cpuspeed=1800&domainid=1&response=json&apikey=fXiib65QMDqStzPerUa0tDOaQ5Zl0GTozBUuLwUKPv5sn7DDhvQ3DRLkteR8eN_BD-HVURJDwnJhiKXgJ9tGTg&signature=eLs%2FDTEz6Gyof4AXSQUSpDc%2B3iA%3D
200
{"createserviceofferingresponse":{"serviceoffering":{"id":"ea53a394-ced6-4ab6-8bfa-1e582c5b1da5","name":"json4c8g","displaytext":"asdasd","cpunumber":4,"cpuspeed":1800,"memory":8096,"created":"2020-09-17T01:43:19+0000","storagetype":"shared","provisioningtype":"thin","offerha":false,"limitcpuuse":false,"isvolatile":false,"domainid":"3e124470-67ff-11e9-ac14-0242ac110002","domain":"ROOT","issystem":false,"defaultuse":false,"miniops":0,"maxiops":0,"iscustomized":false}}}

Python3中调用Cloudstack-API代码相关推荐

  1. ASP.NET MVC4中调用WEB API的四个方法

    当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各种客户端去使用服务已经是十分普遍的做法.就.NET而言,目前提供了Remoting,WebService和WCF服务,这都能开发出功能十分强 ...

  2. 在C#中调用windows API函数

    Api函数是构筑Windws应用程序的基石,每一种Windows应用程序开发工具,它提供的底层函数都间接或直接地调用了Windows API函数,同时为了实现功能扩展,一般也都提供了调用Windows ...

  3. python3中调用map函数报错map object at 0x000001EF004D97B8

    python3中调用map函数报错<map object at 0x000001EF004D97B8> 在python中这样的一段代码报错: a=map(int,input().split ...

  4. Java 中调用 Apache API 实现图片文件的 压缩 与 解压 实例

    < Java 中调用 Apache API 实现图片文件的 压缩 与 解压 > 为什么不直接使用 Java JDK 中自带的 API 呢?必须使用 Apache API 实现文件的压缩与解 ...

  5. C#中调用Windows API时的数据类型对应关系

    C#中调用Windows API时的数据类型对应关系 原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE ...

  6. python把模块装到文件夹中_把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案...

    把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功 解决办法是: 在该文件夹下加入空文件__init__.py python2会把 ...

  7. 把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案...

    把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功 解决办法是: 在该文件夹下加入空文件__init__.py python2会把 ...

  8. Golang cgo:如何在Go代码中调用C语言代码?

    如何在Go代码中调用C语言代码? Go语言是通过自带的一个叫CGO的工具来支持C语言函数调用,同时我们可以用Go语言导出C动态库接口给其它语言使用. 方式一.直接在 Go 代码中写入 C 代码 检查是 ...

  9. C#中调用Windows API的要点

    在.Net Framework SDK文档中,关于调用Windows API的指示比较零散,并且其中稍全面一点的是针对Visual Basic .net讲述的.本文将C#中调用API的要点汇集如下,希 ...

  10. C#中调用Windows API的要点【转载】

    在.Net Framework SDK文档中,关于调用Windows API的指示比较零散,并且其中稍全面一点的是针对Visual Basic .net讲述的.本文将C#中调用API的要点汇集如下,希 ...

最新文章

  1. GetResponse() 基础连接已经关闭:服务器关闭了本应保持活动状态的连接
  2. 两个引用指向同一个数组的内存图
  3. HDU-水饺基情 二维树状数组
  4. Java 11功能– Java飞行记录器
  5. 计算机视觉论文-2021-06-17
  6. 数据工作者的福音:Google 发布正式版数据搜索工具啦!
  7. 模块化配电系统在高密度数据中心的应用
  8. 光热发电成千亿元市场 加速产业布局
  9. 信息安全系统设计基础实验一 2013521120135216
  10. 1995-2019,中国互联网产品的发展与变革
  11. 精神小伙儿探秘JVM( 六)
  12. linux ntfs 3g 格式化,linux使用ntfs-3g操作ntfs格式硬盘
  13. 基于DragonBoard410C的智能管家
  14. 跳出横向的、孤立的牵绊,真正站在新的角度来看待Web3.0
  15. 几款常用的高质量web前端框架
  16. vmware启动虚拟机提示“无法连接虚拟设备ide1:0,主机上没有相对应的设备“
  17. Skipping MapperFactoryBean with name ‘xxx‘ and ‘xxx‘. Bean already defined with the same name’警告
  18. vue修改预设preset
  19. 解决ConstraintLayout两个组件挤压问题
  20. blur事件与click事件的冲突

热门文章

  1. jsp使用rsa加密服务端Java解密
  2. 月考分析五年级英语html,五年级英语上册月考试卷
  3. linux 4g网卡路由,openwrt编译加载龙尚U8300 4G网卡
  4. Centos7安装Torque
  5. (筆記) t_vpi_value struct (SOC) (Verilog PLI)
  6. 在说下一个关于数据库的项目
  7. 计算今天是今年的第几周
  8. 矩阵光学 matlab,矩阵光学.doc
  9. html中单选怎么写,创建一个单选框的html代码是
  10. 洞察了真正的“智能未来”,百度云ABC如何为主导“第四产业”谋局?