python implementation calls the interface Case

# -*- 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--the CP-the API-ID': '2e240f3f', 
#         'X--the CP-the API-KEY': '1ee6ba74f9dd408a85f2', 
#         'X--the ECM-the API-ID': '1,576,043,811,571', 
#         'the ECM-the API-X-KEY-': '02: 42 is: the AC: 14: 00: 07' 
#         } 
# interface server IP port 
IP = ' wepost.cn/wedora ' 
server = ' % S ' % (IP)
 # IP = '172.16.22.21' 
# Port = '50107' 
# Server = '% S:% S'% (IP,Port) 

# interface requests the parameter 
MAC = ' 00: 50: C2: 22 is: 9F: 20 is '

the groupId = ' 2019UG1574834320402 '        # DNS_CHECK 


DEF Save (Result): 
    
    with Open ( ' the result.txt ' , ' A + ' ) AS fw_t: 
        fw_t.write (Result) 
        fw_t.write ( ' \ n- ' ) 

     
DEF get_AllDeviceList (Server, ID , header): 
    
     Print ( " ***** get a list of all device files ****** " ) 
     the Save ( " ***** get a list of all device files ****** " ) 
     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("Complete IS download " ) 



DEF get_OneDeviceInfo (Server, mac, header): 
    
     Print ( " ***** device queries _ a single device ****** " ) 
     the Save ( " ***** _ a single device queries device ****** " ) 
     URL = ' HTTPS: //%s/monitor/amazon-api/v1/device/getSingleDev.hd ' % (Server) 
     the params = {
         ' MAC ' : MAC 
        } 
     R & lt = Requests. GET (URL, the params = the params, headers = header)
      Print (r.text) 
     
     Save (r.text) 


'' '
Get all the device information, if the pass flag = 1, it returns information about the devices; if not pass parameters, return an id. 
All information acquisition device file through the above mentioned id 
'' ' 
     
DEF get_AllDeviceInfo (Server, header): 
    
     Print ( " ***** device queries all devices _ ****** " .center (100 )) 
     the Save ( " *** ** device queries all devices _ ****** " .center (100 )) 
     URL = ' HTTPS: //%s/monitor/amazon-api/v1/device/getAllDev.hd ' % (Server) 
     the params = {
         ' In Flag ' :. 1     # do not pass parameters 
        } 
     R & lt = requests.get (URL, the params = the 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)   # If the transmission parameter is empty, download the device list file 
         # Print ( '---------------------------') 

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

DEF get_DeviceStatus (Server, mac, the above mentioned id, header): 
    
     Print ( " ***** reboot status query groups ** **** " ) 
     Save ( " ***** query groups reboot state ****** " ) 
     URL = ' HTTPS: //%s/monitor/amazon-api/v1/device/devRebootInfo.hd ' % (Server) 
     the params = {
         # ' MAC ': MAC, 
        ' ID ' : ID
        #'ID': post_GroupDeviceReboot (Server, the groupId, header) 
        } 
     R & lt = requests.get (URL, the params = the params, headers = header)
      Print (r.text) 
     
     Save (r.text) 
     

DEF post_OneDeviceReboot (Server, MAC, header) : 
    
     Print ( " ***** a single device, the device is restarted _ ****** " ) 
     Save ( " ***** a single device, the device is restarted _ ****** " ) 
     URL = ' HTTPS: //%s/monitor/amazon-api/v1/device/rebootSingleDev.hd ' % (Server) 
     the params = {
         ' MAC ' : MAC 
        }
     R & lt = requests.post (URL, the params = the 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, the groupId, header): 
    
     Print ( " ***** group of devices reboot _ ****** " .center (100 )) 
     Save ( " ***** reboot device group _ ****** " .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)

 

Guess you like

Origin www.cnblogs.com/relustarry/p/12588521.html