Beispiel #1
0
 def curlHttpRequest(self,
                     url,
                     body,
                     command,
                     headers,
                     isRespHeader,
                     sslVersion=-1):
     headers = toJsonObj(headers)
     command = command.upper()
     isRespHeader = str(isRespHeader).lower() == "true"
     isFormRequest = command.__contains__("FORM")
     if isFormRequest:
         command, body = command.replace("FORM", ''), toJsonObj(body)
         for f in body.keys():
             value = body[f]
             body[f] = self.curlcmd.makeFormValue(f, value, filetype=None)
     return self.curlcmd.curlByCmd(url,
                                   body,
                                   command,
                                   isFormRequest,
                                   headers,
                                   isRespHeader,
                                   isLogResp=False,
                                   logHandler=slog.info,
                                   sslVersion=int(sslVersion))
Beispiel #2
0
def curlCservice(hosts,
                 infPath,
                 isGetInfo=False,
                 isCheckResp=False,
                 logHandler=None,
                 curlHeader={},
                 **args):
    from libs.parser import toJsonStr, toJsonObj
    if isGetInfo:
        resp = curl("%s/cservice/%s" % (hosts, infPath),
                    logHandler=logHandler,
                    **curlHeader)
        try:
            return toJsonObj(resp)
        except:
            return resp

    resp = curl("%s/cservice/%s" % (hosts, infPath),
                toJsonStr(args),
                logHandler=logHandler,
                **curlHeader)
    resp = toJsonObj(resp)
    if isCheckResp:
        if resp[0] != 0:
            raise Exception("Fail to response: %s" % resp[1])
        return resp[1]
    else:
        return resp
Beispiel #3
0
    def addInfData(self,
                   infName,
                   requestArgs,
                   respData,
                   argInfo,
                   argRely,
                   ignoreArgs=[],
                   timeMark=None):
        requestArgs = toJsonObj(requestArgs)
        ignoreArgs = toJsonObj(ignoreArgs)
        argRely = toJsonObj(argRely)

        return self.dapi.saveInfData(infName, argInfo, requestArgs, argRely,
                                     timeMark)
Beispiel #4
0
    def getInfRelys(self, infRely):
        infRely = toJsonObj(infRely)
        infBefores = []
        #         infInfo = infRely
        #         repeatInfo = []
        #         while infInfo is not None:
        #             infName = infInfo["before"]
        #             befArg = infInfo["beforeArg"]
        #
        #             infData = self.__searchInfDataArg__(infName, befArg)
        #             if infData == None:
        #                 break
        #             else:
        #                 infInfo = infData['i']
        #                 repeatMark = "%s%s" % (infName, infInfo)
        #                 if repeatInfo.__contains__(repeatMark):
        #                     break
        #                 else:
        #                     repeatInfo.append(repeatMark)
        #                 infBefores.insert(0, [infName, infData['a'], infInfo])
        #                 infInfo = ObjOperation.tryGetVal(infData, 'r', None)

        #         infName = infRely["after"]
        infAfter = None
        #         infData = self.__searchInfDataArg__(infName, infRely["afterArg"])
        #         if infData is not None:
        #             infAfter = [infName, infData['a'], infData['i']]
        return infBefores, infAfter
Beispiel #5
0
    def executeInfTest(self,
                       hostPort,
                       infName,
                       dataIndex,
                       inProp="",
                       outProp="",
                       replaceProp=None,
                       isDirectReturn=False):
        infCase = self.__getInfData__(infName)[int(dataIndex)]
        infRet = self.doInfRequest(hostPort, infName, infCase['a'],
                                   replaceProp)

        if str(isDirectReturn).lower() == "true":
            return infRet

        try:
            resp = ObjOperation.jsonEqual(
                toJsonObj(infCase['d']),
                infRet,
                isAddEqInfo=True,
                isCmpHandler=lambda key, keyPath: self.__isNeedCheck__(
                    key, keyPath, inProp, outProp))
        except:
            if infCase['d'] == infRet:
                resp = [0, str(infRet)]
            else:
                resp = [1, str(infRet)]

        return list(resp)
Beispiel #6
0
    def curlByCmd(self,
                  url,
                  body=None,
                  command=None,
                  isFormRequest=False,
                  headers=None,
                  isRespHeader=False,
                  isLogResp=True,
                  logHandler=None,
                  sslVersion=-1):
        import time
        from libs.parser import toJsonObj
        from random import randint
        headerFile = (
            "header-%s-%s.txt" %
            (time.time(), randint(10, 1000))) if isRespHeader else None
        curlcmd = self._makeCurlCmd(url, body, command, headers, headerFile,
                                    isFormRequest, sslVersion)
        if logHandler:
            logHandler(curlcmd)

        resp = self.cmdexe(curlcmd)
        if isLogResp and logHandler:
            logHandler(resp)
        try:
            resp = toJsonObj(str(resp))
        except:
            pass

        if isRespHeader:
            return self._parseHeaderFile(headerFile), resp
        else:
            return resp
Beispiel #7
0
    def sendHttpRequest(self,
                        command="POST",
                        url="",
                        header="",
                        body="",
                        bodyArgs="{0},{1}",
                        reqsetting="isRespHeader:true;sslVersion:-1"):
        command = command.upper()
        if bodyArgs != "":
            url = self.__replaceArgStr(url, bodyArgs)
            body = self.__replaceArgStr(body, bodyArgs)
        h = self._toHeaders(header)
        reqsetting = self._toHeaders(reqsetting)

        if command.__contains__("https") or command.__contains__(
                "FORM") or command.__contains__("CURL"):
            command = command.replace("CURL", '')
            resp = self.__curlHttpRequest(
                url, body, command, h,
                tryGet(reqsetting, 'isRespHeader', 'false') == 'true',
                tryGet(reqsetting, 'sslVersion', '-1'))
        else:
            resp = curl(url, body, command=command, logHandler=plog.info, **h)
        try:
            return toJsonObj(resp)
        except:
            return resp
Beispiel #8
0
 def addOnlineTask(self, taskmgr, taskid, taskKey, targs, ttype, hour=-1, minute=0, span=-1, maxCount=-1, runCount=0, opstatus=None, **taskprops):
     task = taskmgr.saveTask(taskKey, 'online', hour, minute, span, maxCount, runCount,
         ttype=ttype, targs=toJsonObj(targs), taskid=taskid)
     for p in taskprops.keys(): task[p] = taskprops[p]
     if opstatus == 'needrun':
         taskmgr.operateTask(taskKey, 'online', 'asyncrun')
     return task
Beispiel #9
0
    def doInfRequest(self,
                     hostPort,
                     infName,
                     requestArgs,
                     replaceProp=None,
                     isHostName=False):
        infPath = infName.replace(".", "/")
        requestArgs = toJsonObj(requestArgs)

        if replaceProp is not None and replaceProp != "" and requestArgs != "":
            try:
                for argName in requestArgs.keys():
                    if replaceProp.__contains__(argName):
                        originVal = requestArgs[argName]
                        requestArgs[argName] = self.replaceValues.TryGet(
                            originVal, originVal)
                requestArgs = requestArgs.__str__(0, True)
            except:
                pass

        return curlCservice(
            self._getServerAddress(hostPort) if isHostName else hostPort,
            infPath,
            isCheckResp=True,
            **requestArgs)
Beispiel #10
0
 def __getRecords(self, fname):
     try:
         records = self.dbBuffer[fname]
     except:
         dbFile = self.dbFileFormat % fname
         try:
             records = toJsonObj(open(dbFile).read())
         except:
             records = []
         self.dbBuffer[fname] = records
     return records
Beispiel #11
0
def curlCservice(hosts, infPath, urlPath=None, isCheckResp=False, isGetInfo=False, logHandler=None, connTimeout=None, **args):
    from libs.parser import toJsonStr, toJsonObj
    if infPath: infPath = infPath.replace(".", "/")
    resp = curl(("%s%s" % (hosts, urlPath)) if urlPath else ("%s/cservice/%s" % (hosts, infPath)), None if isGetInfo else toJsonStr(args), logHandler=logHandler, connTimeout=connTimeout)
    resp = toJsonObj(resp)
    if isCheckResp:
        if resp[0] != 0:
            raise Exception("[%s] %s" % (infPath, resp[1]))
        return resp[1]
    else:
        return resp
Beispiel #12
0
def curlCservice(hosts, infPath, isCheckResp=False, isGetInfo=False, logHandler=None, connTimeout=None, **args):
    from libs.parser import toJsonStr, toJsonObj

    resp = curl("%s/cservice/%s" % (hosts, infPath), None if isGetInfo else toJsonStr(args), logHandler=logHandler, connTimeout=connTimeout)
    resp = toJsonObj(resp)
    if isCheckResp:
        if resp[0] != 0:
            raise Exception("Fail to response: %s" % resp[1])
        return resp[1]
    else:
        return resp
Beispiel #13
0
    def __toHeaders(self, hs):
        try:
            return toJsonObj(hs)
        except:
            pass

        headers = {}
        for hv in hs.split(";"):
            hi = hv.find(":")
            if hi > 0:
                h, v = hv[0:hi].strip(), hv[hi + 1:].strip()
                if len(h) > 0:
                    headers[h] = v
        return headers
Beispiel #14
0
    def sendHttpRequest(self,
                        url,
                        command="POST",
                        body="",
                        bodyArgs="",
                        hostPort=""):
        if bodyArgs != "":
            body = self.__replaceArgStr(body, bodyArgs)
        h = {}
        if hostPort != "":
            hosts, path = self.__splitUrl(url)
            h['Host'] = hosts.split(":")[0]
            url = hostPort + path
        resp = curl(url, body, command=command, logHandler=plog.info, **h)

        try:
            return toJsonObj(resp)
        except:
            return resp
Beispiel #15
0
    def httprequest(
            self,
            command="POST",
            url="",
            header="",
            body="",
            bodyArgs="{0},{1}",
            reqsetting="isRespHeader:true;sslVersion:-1;connTimeout:60",
            isjsonresp=True):
        command = command.upper()
        if bodyArgs != "":
            url = self.__replaceArgStr(url, bodyArgs)
            body = self.__replaceArgStr(body, bodyArgs)
        h = self.__toHeaders(header)
        reqsetting = self.__toHeaders(reqsetting)

        if url.startswith("https") or command.__contains__(
                "FORM") or command.__contains__("CURL"):
            if not command.__contains__("CURL"):
                resp = httpscurl(
                    url, body, command, h,
                    tryGet(reqsetting, 'isRespHeader', 'false') == 'true')
            else:
                command = command.replace("CURL", '')
                resp = self.__curlHttpRequest(
                    url, body, command, h,
                    tryGet(reqsetting, 'isRespHeader', 'false') == 'true',
                    tryGet(reqsetting, 'sslVersion', '-1'))
        else:
            resp = curl(url,
                        body,
                        command=command,
                        logHandler=plog.info,
                        connTimeout=int(tryGet(reqsetting, 'connTimeout',
                                               '60')),
                        **h)
        if isjsonresp:
            try:
                return toJsonObj(resp)
            except:
                pass
        return resp
Beispiel #16
0
    def executeMongo(self,
                     op="find|add|remove",
                     key="",
                     db="",
                     collection="",
                     condition="{}",
                     addContent="{}"):
        if db == "":
            raise Exception("No database set")
        condition = toJsonObj(condition)
        conn = self._getConn(key)
        if collection == "":
            return conn[db].collection_names()

        if op == "find":
            r = conn[db][collection].find(condition)
            return list(r)
        elif op == "remove":
            return conn[db][collection].remove(condition)
        elif op == "add":
            return conn[db][collection].add(addContent)
        else:
            raise Exception("No such operation")
Beispiel #17
0
 def statFolder(self, hostport, path):
     return toJsonObj(curl("%s/file/%s?type=stat" % (hostport, path)))
Beispiel #18
0
 def convertJsonToUrlParam(self, jsonStr):
     return jsonToUrlValue(toJsonObj(jsonStr))
Beispiel #19
0
 def operateTask(self, tkey, optype, rargs=None):
     return self._taskmgr.operateTask(tkey, "online", optype, toJsonObj(rargs))
Beispiel #20
0
    def sendHttpsRequst(self,
                        url,
                        body,
                        command=None,
                        headers={},
                        isRespHeader=False,
                        certFile=None):
        import httplib
        import socket
        import ssl
        httpsConn = None
        certFile = None

        hosts, path = self.__splitUrl(url)
        try:
            httpsConn = httplib.HTTPSConnection(hosts)
            sock = socket.create_connection((httpsConn.host, httpsConn.port))
            try:
                httpsConn.sock = ssl.wrap_socket(
                    sock,
                    ca_certs=certFile,
                    cert_reqs=ssl.CERT_NONE,
                    ssl_version=ssl.PROTOCOL_SSLv23)
            except ssl.SSLError:
                try:
                    httpsConn.sock = ssl.wrap_socket(
                        sock,
                        ca_certs=certFile,
                        cert_reqs=ssl.CERT_NONE,
                        ssl_version=ssl.PROTOCOL_SSLv2)
                except ssl.SSLError:
                    try:
                        httpsConn.sock = ssl.wrap_socket(
                            sock,
                            ca_certs=certFile,
                            cert_reqs=ssl.CERT_NONE,
                            ssl_version=ssl.PROTOCOL_TLSv1)
                    except ssl.SSLError:
                        try:
                            httpsConn.sock = ssl.wrap_socket(
                                sock,
                                ca_certs=certFile,
                                cert_reqs=ssl.CERT_NONE,
                                ssl_version=ssl.PROTOCOL_SSLv3)
                        except Exception as ex:
                            slog.info("ssl version %s: %s" % (ex, url))

            httpsConn.request(command, path, body, headers)
            res = httpsConn.getresponse()

            resHeaders = {}
            for k, v in res.getheaders():
                resHeaders[k] = v
            body = res.read()
            try:
                body = toJsonObj(body)
            except:
                pass
            if isRespHeader:
                return resHeaders, body
            else:
                return body
        finally:
            if httpsConn:
                httpsConn.close
Beispiel #21
0
 def jsonToUrlargs(self, jsonStr):
     return jsonToUrlValue(toJsonObj(jsonStr))
Beispiel #22
0
 def searchInfData(self, infName, infCond={}):
     infCond = toJsonObj(infCond)
     return self.dapi.getInfData(infName, infCond)