python实现调用接口案例

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 11 08:35:29 2019

@author: xbxia
"""


import requests
import json


header = {
        'X-CP-API-ID':'343293f1',
        'X-CP-API-KEY':'7f667830890b4374b8c7',
        'X-ECM-API-ID':'1576580160178',
        'X-ECM-API-KEY':'02:F9:83:D1:6D:D6'
        }


#header = {
#        'X-CP-API-ID':'2e240f3f',
#        'X-CP-API-KEY':'1ee6ba74f9dd408a85f2',
#        'X-ECM-API-ID':'1576043811571',
#        'X-ECM-API-KEY':'02:42:AC:14:00:07'
#        }
# 接口服务器IP 端口
ip = 'wepost.cn/wedora'
server = '%s'%(ip)
#ip = '172.16.22.21'
#port = '50107'
#server = '%s:%s'%(ip,port)

# 接口请求参数
mac = '00:50:C2:22:9F:20'

groupId = '2019UG1574834320402'       #DNS_CHECK


def save(result):
    
    with open('result.txt', 'a+') as fw_t:
        fw_t.write(result)
        fw_t.write('\n')

     
def get_AllDeviceList(server,id,header):
    
     print("*****获取所有设备列表文件******")
     save("*****获取所有设备列表文件******")
     url = 'https://%s/monitor/amazon-api/v1/device/devListFile.hd'%(server)
     params = {
        'id':id
        #'id':get_AllDeviceInfo(server,header)
        }
     print("start with downloading file")
     r = requests.get(url, params=params, headers=header)
     with open("DeviceList.zip", "wb") as code:
         code.write(r.content)
     print("download is complete")
     save("download is complete")



def get_OneDeviceInfo(server,mac,header):
    
     print("*****设备查询_单台设备******")
     save("*****设备查询_单台设备******")
     url = 'https://%s/monitor/amazon-api/v1/device/getSingleDev.hd'%(server)
     params = {
        'mac':mac
        }
     r = requests.get(url, params=params, headers=header)
     print(r.text)
     
     save(r.text)


'''
获取所有设备信息,若传flag=1,则返回所有设备信息;若不传参数,则返回一个id。
通过id获取设备所有信息文件
'''
     
def get_AllDeviceInfo(server,header):
    
     print("*****设备查询_所有设备******".center(100))
     save("*****设备查询_所有设备******".center(100))
     url = 'https://%s/monitor/amazon-api/v1/device/getAllDev.hd'%(server)
     params = {
        'flag':1    #不传参
        }
     r = requests.get(url, params=params, headers=header)
    
     #print(json.loads(r.text)['result']['downLoadUrl'].split('=')[1])
     #print('*********************************')
     #print(params)
     if params != {}:
         print(r.text)
         save(r.text)
     else:
         print(r.text)
         id = json.loads(r.text)['result']['downLoadUrl'].split('=')[-1]
         #print(id)
         save(r.text)
         get_AllDeviceList(server,id,header)   #如果传参为空,就下载设备列表文件
         #print('---------------------------')

     
 ##############################################################################

def get_DeviceStatus(server,mac,id,header):
    
     print("*****查询组设备重启状态******")
     save("*****查询组设备重启状态******")
     url = 'https://%s/monitor/amazon-api/v1/device/devRebootInfo.hd'%(server)
     params = {
        #'mac':mac,
        'id':id
       # 'id':post_GroupDeviceReboot(server,groupId,header)
        }
     r = requests.get(url, params=params, headers=header)
     print(r.text) 
     
     save(r.text)
     

def post_OneDeviceReboot(server,mac,header):
    
     print("*****设备重启_单台设备******")
     save("*****设备重启_单台设备******")
     url = 'https://%s/monitor/amazon-api/v1/device/rebootSingleDev.hd'%(server)
     params = {
        'mac':mac
        }
     r = requests.post(url, params=params, headers=header)
     print(r.text)
     save(r.text)
    # print('*********************************')
     id = json.loads(r.text)['result']['id']

     #return(id)
     get_DeviceStatus(server,mac,id,header)
     
    

def post_GroupDeviceReboot(server,mac,groupId,header):
    
     print("*****设备重启_组设备******".center(100))
     save("*****设备重启_组设备******".center(100))
     url = 'https://%s/monitor/amazon-api/v1/device/rebootGroupDev.hd'%(server)
     params = {
        'groupId':groupId
        }
     r = requests.post(url, params=params, headers=header)
     print(r.text)
     save(r.text)
     #print('*********************************')
     id = json.loads(r.text)['result']['id']
     #return(id)   #返回任务id
     get_DeviceStatus(server,mac,id,header)
     
         


if __name__ == '__main__':
    
    #get_OneDeviceInfo(server,mac,header)
    get_AllDeviceInfo(server,header)
    #post_OneDeviceReboot(server,mac,header)
    #post_GroupDeviceReboot(server,mac,groupId,header)
   
    #get_DeviceStatus(server,mac,1576585002185,header)

 

posted @ 2020-03-28 17:56  ReluStarry  阅读(1174)  评论(0编辑  收藏  举报