Пример #1
0
    def _sendfile(self, filename="", data="", load="n"):

        if (filename.startswith('/')):
            xurl = self.baseurl + "/file/upload" + filename + "?load=" + load
        else:
            xurl = self.baseurl + "/file/upload/" + filename + "?load=" + load

        if self.debug & 0x02:
            print("{0} xurl : {1}".format(__name__, xurl))
        # req = URL.Request(xurl, data, {'Content-Type': 'application/xml; charset="utf-8"'})
        req_headers = {'Content-Type': 'application/xml; charset="utf-8"'}

        try:
            res = self._req_session.post(xurl, data=data, headers=req_headers)
            responce = res.text
            res.close()
            # print("responce:", res.status_code, len(responce))
        except requests.exceptions.RequestException as rex:
            mess = "{!s} : {!s} : {!s}".format("/file/upload", filename,
                                               rex.response.status_code)
            raise IsyE.IsySoapError(mess, httperr=rex)
        else:
            return responce
Пример #2
0
    def soapcomm(self, cmd, **kwargs):
        """
        takes a command name and a list of keyword arguments.
        each keyword is converted into a xml element
        """

        if not isinstance(cmd, str) or not cmd:
            raise IsyE.IsyValueError("SOAP Method name missing")

        # if self.debug & 0x02:
        #     print("sendcomm : ", cmd)

        soap_cmd = self._gensoap(cmd, **kwargs)

        xurl = self.baseurl + "/services"
        if self.debug & 0x02:
            # print("xurl = ", xurl)
            print("soap_cmd = ", soap_cmd)

        # req_headers = {'content-type': 'application/soap+xml'}
        # req_headers = {'content-type': 'text/xml'}
        req_headers = {'Content-Type': 'application/xml; charset="utf-8"'}

        # req = URL.Request(xurl, soap_cmd, {'Content-Type': 'application/xml; charset="utf-8"'})

        data = ""
        try:

            res = self._req_session.post(xurl,
                                         data=soap_cmd,
                                         headers=req_headers)
            data = res.text  # res.content
            if self.debug & 0x200:
                print("res.status_code ", res.status_code, len(data))
                print("data ", data)
            res.close()

        # except URL.HTTPError as e:
        except requests.exceptions.RequestException as rex:

            status_code = rex.response.status_code
            self.error_str = str("Reponse Code : {0} : {1} {2}").format(
                status_code, xurl, cmd)
            if ((cmd == "DiscoverNodes" and rex.response.status_code == 803)
                    or (cmd == "CancelNodesDiscovery" and status_code == 501)
                    # or (cmd == "RemoveNode" and status_code == 501)
                ):

                if self.debug & 0x02:
                    print("spacial case : {0} : {1}".format(cmd, status_code))
                    print("status_code = ", status_code)
                    print("response.reason = ", rex.response.reason)
                    print("response.headers = ", rex.response.headers)
                    # print("e.filename = ", e.filename)
                    print("\n")

                return rex.response.text

            if self.debug & 0x202:
                print("status_code = ", status_code)
                # print("e.read = ", e.read())
                print("RequestException = ", rex)
                print("data = ", data)

            mess = "{!s} : {!s} : {!s}".format(cmd, kwargs, status_code)
            # This a messy and should change
            raise IsyE.IsySoapError(mess, httperr=rex)
        else:
            if self.error_str:  # len
                self.error_str = ""
            if self.debug & 0x200:
                print(data)
            return data