python调用自定义webapi原创
金蝶云社区-CecilSunshine
CecilSunshine
10人赞赏了该文章 1478次浏览 未经作者许可,禁止转载编辑于2022年01月17日 16:19:01


# -*- coding:utf-8 -*-

import urllib.request as urllib2

import uuid

import datetime

import json

import http.cookiejar

import os

def getDataCenterInfo():

    return ["**********","username","password","2052"]


def getHeaders():

    headers={"Content-Type":"application/json","Connection":"KeepAlive","Accept-Charset":"utf-8",}

    return headers


def buildCommonParameters():

    param = {}

    param["format"]=1

    param["useragent"]="ApiClient"

    param["rid"]=str(uuid.uuid1().__hash__())

    now = datetime.datetime.now()

    param["timestamp"]= now.strftime('%Y-%m-%d %H:%M:%S') 

    param["v"]="1.0"

    return param


def login(cj):

    url = 'http://localhost:1800/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc'

    pp= getDataCenterInfo()

    param = buildCommonParameters()

    param["parameters"]=pp

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

    print(str(param))

    req = urllib2.Request(url,str(param).encode(encoding='utf-8'),getHeaders(),'POST')

    response = opener.open(req)

    rp = json.loads(response.read().decode('utf-8'))

    print(rp)

    if rp["LoginResultType"]==1:

        return True

    else:

        return False


def getBomQuery(cj):

    url="http://localhost:1800/k3cloud/WebApiService.GetBomExpandService.ExecuteService,WebApiService.common.kdsvc"

    param=buildCommonParameters()

    p={"ValidDate":"2022/01/10","BomIds":"155725","Qty":"1"}

    req = urllib2.Request(url,json.dumps({"info":{"ValidDate":"2022/01/10","BomIds":[155725],"Qty":"1"}}).encode(encoding='utf-8'),getHeaders(),'POST')

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

    response = opener.open(req)

    rp = json.loads(response.read().decode('utf-8'))

    print(rp)

    m = json.dumps(rp)

    mtrl = open("D:\\mmp.txt","w+")

    mtrl.write(m)

    mtrl.close()


cj = http.cookiejar.CookieJar()

login(cj)

getBomQuery(cj)


赞 10