itop api 调用开发方法

import urllib
#import urllib2
import urllib.request, urllib.error
import json
 
def http_post(operations):
    url="http://10.12.7.11/mdcmdb/webservices/rest.php?version=1.3"
    auth ={"auth_user": "username" , "auth_pwd": "dsffs"}
    #opr = {"operation": "list_operations"}  
    opr = operations
 
    oprjson =urllib.parse.urlencode({'json_data': json.dumps(opr)}  ).encode("utf-8")
    jdata = urllib.parse.urlencode(auth).encode("utf-8")
    jdata = jdata+b'&'+oprjson
 
    response = urllib.request.urlopen(url,jdata)
    return response.read().decode()
     
def get_organization(friendlyname):
    getopr = {
            "operation": "core/get",
            "class": "Organization",
            "key": "SELECT Organization WHERE  friendlyname='"+friendlyname+"'",
        }
 
    tmp=http_post(getopr)
    result=json.loads(str(tmp))
    if int(result["message"].split()[1]) >= 1 :
        return 0
    else:
        return 1
 
    return result
 
def add_new_organization(friendlyname,parent_friendlyname):
    createopr = {
            "operation": "core/create",
            "class": "Organization",
            "comment": "add new  Organization",
            "output_fields" : "*",
            "fields":
            {
            "friendlyname":friendlyname,
            "name":friendlyname,
            "parent_id":"SELECT Organization WHERE  friendlyname='"+parent_friendlyname+"'",
            "parent_id_friendlyname":parent_friendlyname,
            }
        }
    result=http_post(createopr)
    return result

你可能感兴趣的:(itop api 调用开发方法)