Todd Howe
Todd Howe

Reputation: 77

How Do I Coax PySimpleSOAP to Form Requests like PHP's SoapClient?

I'm writing a series of scripts to automate tasks on ISPConfig, the web hosting console. Which you don't need to know about, really, because this question's about forming XML. For ... reasons ... I want to accomplish this task in Python rather than PHP. My first script's purpose is to create new mailboxes for a given domain, and I had some existing PHP code to work off of.

Things were going well. Despite the somewhat terse documentation, I'd gotten to a point where my script was communicating with the API, and sending along parameters, with pysimplesoap calls like the following.

client = pysimplesoap.client.SoapClient(location=SOAP_LOCATION,
                                        namespace=SOAP_URI, trace=True)
response = client.login(username=console_login, password=console_pass)
session_id = str(response.children().children().children())
params = {
          'server_id': 1,
          'email': mailbox.mail_id,
          'login': mailbox.mail_id,
          'password': mailbox.mail_pass,
           # plus more params here ...
         }
response = client.mail_user_add(session=session_id,
                                client=client_id, params=params)
client.logout(session=session_id)

No errors were thrown and login and logout worked, however checking back with the ISPConfig console, a totally blank mailbox was created. Meanwhile, the identical calls in PHP produce populated mailboxes.

After getting both languages to spew their respective responses and requests to the screen, it became pretty clear what the issue was...

Python SOAP request to server (using code above)

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n
<soap:Header/>\n
<soap:Body>\n    
    <mail_user_add xmlns="https://localhost:8080/remote/">\n  
    <session>8a7d6bc047a299974385f7e988570bc4</session>
    <params>
        <server_id>1</server_id>
        <email>[email protected]</email>
        <login>[email protected]</login>
        <blah>the rest of the params...</blah>
    </params>
    <client>0</client>
    </mail_user_add>\n
</soap:Body>\n
</soap:Envelope>

PHP SOAP request to server

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://66.212.164.9:8080/remote/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Body>
<ns1:mail_user_add>
<param0 xsi:type="xsd:string">b0048bd548a3062a3dbcbc49bc74a39c</param0>
<param1 xsi:type="xsd:int">0</param1>
<param2 xsi:type="ns2:Map">
    <item>
        <key xsi:type="xsd:string">server_id</key>
        <value xsi:type="xsd:int">1</value>
    </item>
    <item>
        <key xsi:type="xsd:string">email</key>
        <value xsi:type="xsd:string">[email protected]</value>
    </item>
    <item>
        <key xsi:type="xsd:string">login</key>
        <value xsi:type="xsd:string">[email protected]</value>
    </item>
    <item> ... </item>
</param2>
</ns1:mail_user_add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Well. Damn. How should I go about making pysimplesoap create requests in the format above so they can be properly consumed by the API?

(Unless it emerges it's a hopeless case, I'm using the updated pysimplesoap 1.16 from https://github.com/pysimplesoap/pysimplesoap Also python3)

PS It looks like ISPConfig unfortunately does not supply the mysterious document known as the WSDL which I've just started reading up on. This is probably important to know, and rules out the suds-jurko client as a plan B, though it looks nice too. Cite no WSDL: http://bugtracker.ispconfig.org/index.php?do=details&task_id=1273 Cite can't use suds: https://lists.fedoraproject.org/pipermail/suds/2010-February/000702.html

Upvotes: 1

Views: 503

Answers (0)

Related Questions