tags:

views:

706

answers:

2

I have a SOAP request that is known to work using a tool like, say, SoapUI, but I am trying to get it to work using urllib.

This is what I have tried so far and it did not work:

import urllib
f = "".join(open("ws_request_that_works_in_soapui", "r").readlines())
urllib.urlopen('http://url.com/to/Router?wsdl', f)

I haven't been able to find the spec on how the document should be posted to the SOAP Server.

urllib is not a necessary requirement.

+2  A: 

Well, I answered my own question

import httplib

f = "".join(open('ws_request', 'r'))

webservice = httplib.HTTP('localhost', 8083)
webservice.putrequest("POST", "Router?wsdl")
webservice.putheader("User-Agent", "Python post")
webservice.putheader("Content-length", "%d" % len(f))
webservice.putheader("SOAPAction", "\"\"")
webservice.endheaders()
webservice.send(f)
Nick Stinemates
It's probably the SOAPAction and Content-length headers that were tripping you up before. Note that adding headers is super-simple using urllib2 as well - IMHO nicer to use than httplib.
Alabaster Codify
+2  A: 

Short answer: yes you can.

Long answer:

Take a look at this example it doesn't use urllib but will give you the idea of how to prepare SOAP request.

As far as urllib, I suggest using urllib2, and yes you can submit a SOAP request using it, follow the same steps to prepare the request as in previous example.