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://ift.tt/sVJIaE" xmlns:xsd="http://ift.tt/tphNwY" xmlns:xsi="http://ift.tt/ra1lAU">\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>someone@domain.com</email>
<login>someone@domain.com</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://ift.tt/sVJIaE" xmlns:ns1="http://ift.tt/1CZei5N" xmlns:xsd="http://ift.tt/tphNwY" xmlns:xsi="http://ift.tt/ra1lAU" xmlns:ns2="http://ift.tt/1a5EBb7" xmlns:SOAP-ENC="http://ift.tt/wEYywg" SOAP-ENV:encodingStyle="http://ift.tt/wEYywg">

<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">someone@domain.com</value>
</item>
<item>
<key xsi:type="xsd:string">login</key>
<value xsi:type="xsd:string">someone@domain.com</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? I'd rather not switch tools unless this is a hopeless case since from what I've read the ones I'm using are the most well-maintained. Thanks!


(I'm using python3 and the updated pysimplesoap 1.16 from http://ift.tt/1lU1XsD)


PS Should I strip out those '\n's as well or are they harmless...


No comments:

Post a Comment