简体   繁体   中英

How to add username and password for xml format with python

def test(login,password):
    data="""<?xml version="1.0"?>
        <soap-env>
        <soap-env:Body>
        <Auth>
        <Login>login</Login><password>password</password>
        </Auth>
        <Ping>
        </Ping>
        </soap-env:Body>
        </soap-env:Envelope>"""
    return data

i can't add login and password when i use '+login+' and i can't do format string like this " "

Try the below ('f' string)

def test(login,password):
    data=f"""<?xml version="1.0"?>
        <soap-env>
        <soap-env:Body>
        <Auth>
        <Login>{login}</Login><password>{password}</password>
        </Auth>
        <Ping>
        </Ping>
        </soap-env:Body>
        </soap-env:Envelope>"""
    return data

print(test('jack','secret'))

output

<?xml version="1.0"?>
        <soap-env>
        <soap-env:Body>
        <Auth>
        <Login>jack</Login><password>secret</password>
        </Auth>
        <Ping>
        </Ping>
        </soap-env:Body>
        </soap-env:Envelope>

If you see that data is XML you have to use XML module to build it. Building string which looks like XML is not a good idea as it can easily violate standard. Here is how it can (should) be done using built-in xml.etree.ElementTree :

import xml.etree.ElementTree as ET

def test(login, password):
    envelope_uri = "http://schemas.xmlsoap.org/soap/envelope/"
    ET.register_namespace("soap-env", envelope_uri)
    envelope_node = ET.Element(ET.QName(envelope_uri, "Envelope"))
    body_node = ET.SubElement(envelope_node, ET.QName(envelope_uri, "Body"))
    auth_node = ET.SubElement(body_node, "Auth")
    ET.SubElement(auth_node, "Login").text = login
    ET.SubElement(auth_node, "Password").text = password
    ET.SubElement(body_node, "Ping")

    # ET.indent(envelope_node)  # uncomment this to get indented output (python 3.9+)
    return ET.tostring(
        envelope_node,  # root node
        "utf-8",  # encoding
        xml_declaration=True,  # add xml declaration on top
        short_empty_elements=False  # use start/end pair for empty nodes
    ).decode()

print(test("login",  "p<>&sword"))

This code will produce next output:

<?xml version='1.0' encoding='utf-8'?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Body>
    <Auth>
      <Login>login</Login>
      <Password>p&lt;&gt;&amp;sword</Password>
    </Auth>
    <Ping></Ping>
  </soap-env:Body>
</soap-env:Envelope>

This is valid XML ( proof ) .

Function from accepted answer with same arguments will produce next output:

<?xml version="1.0"?>
        <soap-env>
        <soap-env:Body>
        <Auth>
        <Login>login</Login><password>p<>&sword</password>
        </Auth>
        <Ping>
        </Ping>
        </soap-env:Body>
        </soap-env:Envelope>

This is invalid XML ( proof ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM