Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Python XML DOM解析器无法解析有效的XML_Python_Xml_Parsing_Soap - Fatal编程技术网

我的Python XML DOM解析器无法解析有效的XML

我的Python XML DOM解析器无法解析有效的XML,python,xml,parsing,soap,Python,Xml,Parsing,Soap,我目前正在尝试构建一个简单的python脚本,以自动完成登录到基于SOAP XML的Web服务的繁琐任务,多次调用相同的方法,最后注销 然而,我在构建XMLDOM时遇到了一些问题,仅仅是因为解析器出错 以下是我目前正在使用的代码 #!/usr/bin/env python3 import http.client import urllib.parse import xml.dom.minidom class soap_consumer: def __init__(self,

我目前正在尝试构建一个简单的python脚本,以自动完成登录到基于SOAP XML的Web服务的繁琐任务,多次调用相同的方法,最后注销

然而,我在构建XMLDOM时遇到了一些问题,仅仅是因为解析器出错

以下是我目前正在使用的代码

#!/usr/bin/env python3

import http.client
import urllib.parse
import xml.dom.minidom

class soap_consumer:

        def __init__(self, msg, json=False):
                self.msg = msg
                self.json = json

        def envelope(self):
                if self.json:
                        return self.msg
                else:
                        doc = xml.dom.minidom.Document()
                        env = doc.createElement('soapenv:Envelope')
                        env.setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
                        env.setAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema')
                        env.setAttribute('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/')
                        env.setAttribute('xmlns:urn', 'urn:packetfront_becs')

                        print(self.msg)

                        #XML input request data
                        rawdom = xml.dom.minidom.parseString(self.msg)
                        messagenode = rawdom.firstChild

                        #Header
                        header = doc.createElement('soapenv:Header')
                        env.appendChild(header)

                        #Body
                        body = doc.createElement('soapenv:Body')
                        body.appendChild(messagenode)

                        env.appendChild(body)
                        doc.appendChild(env)

                        print(doc.toxml('utf-8'))

                        return doc.toxml('utf-8')

        def send_request(self, url, port, path, content_type, accept, https=False):
                data = self.envelope()

                print(data)
                print(len(data))

                headers = {"Content-type" : content_type, "Accept": accept, "Content-length": len(data)}
                conn = ''

                conn = http.client.HTTPConnection(url, port)

                print(conn)
                conn.request("POST", path, data, headers)

                response = conn.getresponse()
                resp_data = response.read()

                print(resp_data)

                if response.status == 200:
                        conn.close()
                        return resp_data
                else:
                        return 'Failed:' + str(response.status) + str(resp_data)
                        

login_req = """
<urn:sessionLogin soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <in xsi:type="urn:sessionLoginIn">
    <!--You may enter the following 3 items in any order-->
    <username xsi:type="xsd:string">USERNAME</username>
    <password xsi:type="xsd:string">PASSWORD</password>
  </in>
</urn:sessionLogin>
"""

sw_login_req = soap_consumer(login_req)
sw_resp = sw_login_req.send_request(url='URL-REDACTED', port='80', path='/', content_type='application/soap+xml; charset=utf-8', accept='text/xml')
#/usr/bin/env蟒蛇3
导入http.client
导入urllib.parse
导入xml.dom.minidom
类别soap\U消费者:
def _; init _;(self,msg,json=False):
self.msg=msg
self.json=json
def信封(自我):
如果self.json:
返回self.msg
其他:
doc=xml.dom.minidom.Document()
env=doc.createElement('soapenv:Envelope')
setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')
setAttribute('xmlns:xsd','http://www.w3.org/2001/XMLSchema')
setAttribute('xmlns:soapenv','http://schemas.xmlsoap.org/soap/envelope/')
环境setAttribute('xmlns:urn','urn:packetfront_becs')
打印(self.msg)
#XML输入请求数据
rawdom=xml.dom.minidom.parseString(self.msg)
messagenode=rawdom.firstChild
#标题
header=doc.createElement('soapenv:header')
环境追加子项(标题)
#身体
body=doc.createElement('soapenv:body')
body.appendChild(messagenode)
环境附体(主体)
附录文件(环境)
打印(文件toxml('utf-8'))
退货单据toxml('utf-8')
def发送请求(self、url、端口、路径、内容类型、接受、https=False):
data=self.envelope()
打印(数据)
打印(透镜(数据))
headers={“内容类型”:内容类型,“接受”:接受,“内容长度”:len(数据)}
康涅狄格州
conn=http.client.HTTPConnection(url,端口)
打印(康涅狄格州)
连接请求(“POST”、路径、数据、标题)
response=conn.getresponse()
resp_data=response.read()
打印(响应数据)
如果response.status==200:
康涅狄格州关闭
返回响应数据
其他:
返回“失败:”+str(response.status)+str(resp_数据)
登录请求“”
用户名
密码
"""
sw_login_req=soap_消费者(login_req)
sw_resp=sw_login_req.send_请求(url='url-redact',port='80',path=''/',content_type='application/soap+xml;charset=utf-8',accept='text/xml')
但我得到了一个解析错误

Traceback (most recent call last):
  File "./update_interfaces.py", line 148, in <module>
    sw_resp = sw_login_req.send_request(url='URL REDACTED', port='4490', path='/', content_type='application/soap+xml; charset=utf-8', accept='text/xml')
  File "./update_interfaces.py", line 46, in send_request
    data = self.envelope()
  File "./update_interfaces.py", line 27, in envelope
    rawdom = xml.dom.minidom.parseString(self.msg)
  File "/usr/lib/python3.6/xml/dom/minidom.py", line 1968, in parseString
    return expatbuilder.parseString(string)
  File "/usr/lib/python3.6/xml/dom/expatbuilder.py", line 925, in parseString
    return builder.parseString(string)
  File "/usr/lib/python3.6/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: unbound prefix: line 2, column 0
回溯(最近一次呼叫最后一次):
文件“/update_interfaces.py”,第148行,在
sw_resp=sw_login_req.send_请求(url='url REDACTED',port='4490',path=''/',content_type='application/soap+xml;charset=utf-8',accept='text/xml')
文件“/update\u interfaces.py”,第46行,在发送请求中
data=self.envelope()
文件“/update_interfaces.py”,第27行,信封中
rawdom=xml.dom.minidom.parseString(self.msg)
文件“/usr/lib/python3.6/xml/dom/minidom.py”,第1968行,格式为parseString
返回expatbuilder.parseString(字符串)
文件“/usr/lib/python3.6/xml/dom/expatbuilder.py”,第925行,格式为parseString
return builder.parseString(字符串)
文件“/usr/lib/python3.6/xml/dom/expatbuilder.py”,第223行,格式为parseString
parser.Parse(字符串,True)
xml.parsers.expat.expat错误:未绑定前缀:第2行第0列
我已经通过postman测试了原始XML,它按照预期工作,如下所示:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:packetfront_becs">
   <soapenv:Header>
      <request xsi:type="urn:requestHeader">
         <!--You may enter the following 2 items in any order-->
         <sessionid xsi:type="xsd:string"/>
      </request>
   </soapenv:Header>
   <soapenv:Body>
      <urn:sessionLogin soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <in xsi:type="urn:sessionLoginIn">
            <!--You may enter the following 3 items in any order-->
            <username xsi:type="xsd:string">USERNAME</username>
            <password xsi:type="xsd:string">PASSWORD</password>
         </in>
      </urn:sessionLogin>
   </soapenv:Body>
</soapenv:Envelope>

用户名
密码
我做错了什么