コード例 #1
0
    def xml_rawquery(self, xml_str, dump_xml=None):
        """
        Accepts xmlQuery String and returns XML response String. No object manipulation is done in this method.
        """

        if dump_xml == None:
            dump_xml = self.__dump_xml

        uri = self.uri() + '/nuova'
        if dump_xml in ImcUtils.AFFIRMATIVE_LIST:
            print '%s ====> %s' % (self.__imc, xml_str)

        xml_doc = ET.fromstring(xml_str)

        if self.__nossl:
            req = urllib2.Request(url=uri, data=ET.tostring(xml_doc))
            opener = urllib2.build_opener(ImcUtils.SmartRedirectHandler())
            resp = opener.open(req)

            if type(resp) is list:
                if len(resp) == 2 and (resp[0] == 302 or resp[0] == 301):
                    uri = resp[1]
                    req = urllib2.Request(url=uri, data=ET.tostring(xml_doc))
                    resp = urllib2.urlopen(req)
                    #print "status code is:",f[0]
                    #print "location is:", f[1]
        else:
            req = urllib2.Request(url=uri, data=ET.tostring(xml_doc))
            resp = urllib2.urlopen(req)

        rsp = resp.read()
        if dump_xml in ImcUtils.AFFIRMATIVE_LIST:
            print '%s <==== %s' % (self.__imc, rsp)
        return rsp
コード例 #2
0
    def xml_query(self, method, options, dump_xml=None):
        """
        xml_query method opens a connection to URL, sends the xmlQuery string to URL location,
        and returns result.
        Prepares objects from XML query result and returns external method response.
        """
        #import sys
        #if not sys.version_info < (2, 5):
        #    from functools import partial
        #    import ssl
        #    ssl.wrap_socket = partial(ssl.wrap_socket, ssl_version=ssl.PROTOCOL_TLSv1)

        if dump_xml == None:
            dump_xml = self.__dump_xml

        try:
            #xml_doc = xml.dom.minidom.Document()
            #xml_doc.appendChild(method.write_xml(xml_doc, options))
            #print method.__dict__
            xml_doc = method.write_xml(option=options)

            uri = self.uri() + '/nuova'
            if dump_xml in ImcUtils.AFFIRMATIVE_LIST:
                print '%s ====> %s' % (self.__imc, ET.tostring(xml_doc))

            if self.__proxy is None:
                if self.__nossl:
                    req = urllib2.Request(url=uri, data=ET.tostring(xml_doc))
                    opener = urllib2.build_opener(
                        ImcUtils.SmartRedirectHandler())
                    resp = opener.open(req)
                    if type(resp) is list:
                        if len(resp) == 2 and (resp[0] == 302
                                               or resp[0] == 301):
                            uri = resp[1]
                            req = urllib2.Request(url=uri,
                                                  data=ET.tostring(xml_doc))
                            resp = urllib2.urlopen(req)
                else:
                    req = urllib2.Request(url=uri, data=ET.tostring(xml_doc))
                    opener = urllib2.build_opener(ImcUtils.TLS1Handler())
                    resp = opener.open(req)
            else:
                proxy_handler = urllib2.ProxyHandler({
                    'http': self.__proxy,
                    'https': self.__proxy
                })

                if self.__nossl:
                    req = urllib2.Request(url=uri, data=ET.tostring(xml_doc))
                    opener = urllib2.build_opener(
                        proxy_handler, ImcUtils.SmartRedirectHandler())
                    resp = opener.open(req)

                    if type(resp) is list:
                        if len(resp) == 2 and (resp[0] == 302
                                               or resp[0] == 301):
                            uri = resp[1]
                            req = urllib2.Request(url=uri,
                                                  data=ET.tostring(xml_doc))
                            opener = urllib2.build_opener(proxy_handler)
                            resp = opener.open(req)
                else:
                    req = urllib2.Request(url=uri, data=ET.tostring(xml_doc))
                    opener = urllib2.build_opener(ImcUtils.TLS1Handler(),
                                                  proxy_handler)
                    resp = opener.open(req)

            rsp = resp.read()

            if dump_xml in ImcUtils.AFFIRMATIVE_LIST:
                print '%s <==== %s' % (self.__imc, rsp)

            response = ExternalMethod(method.prop_mo_meta.name)
            root_element = ET.fromstring(rsp)
            response.load_from_xml(root_element, self)
            return response
        except Exception:
            raise