Python and SOAP / WSDL

# SOAP/WSDL — This is a Python 2.7 version of a sample code consuming a Web Service
# ==========
import requests

#url=”http://www.webservicex.net/ConvertSpeed.asmx?WSDL” # No authentication needed
#url=”http://servername:serverport/yourcontext/your/path/F1-COUNTRY?WSDL” # No authentication needed
url=”http://servername:serverport/yourcontext/your/path/F1-COUNTRY”

headers = {‘content-type’: ‘text/xml’}
#headers = {‘content-type’: ‘text/xml’,
#           ‘Authorization’: ‘Basic blablabla==’}
#headers = {‘content-type’: ‘text/xml’,
#           ‘Authorization’: ‘Basic blublublu==’}

proxies = {“http”: “http://proxy:port”,
“https”: “https://proxy:port”,
“socks”: “socks://proxy:port”,
“ftp”: “ftp://proxy:port”}

#body = “””<?xml version=”1.0″ encoding=”utf-8″?>
#<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema&#8221; xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”&gt;
#  <soap:Body>
#    <ConvertSpeed xmlns=”http://www.webserviceX.NET/”&gt;
#      <speed>100</speed>
#      <FromUnit>kilometersPerhour</FromUnit>
#      <ToUnit>milesPerhour</ToUnit>
#    </ConvertSpeed>
#  </soap:Body>
#</soap:Envelope>”””

#body = “””<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:web=”http://www.webserviceX.NET/”&gt;
#   <soapenv:Header/>
#   <soapenv:Body>
#      <web:ConvertSpeed>
#         <web:speed>100</web:speed>
#         <web:FromUnit>kilometersPerhour</web:FromUnit>
#         <web:ToUnit>milesPerhour</web:ToUnit>
#      </web:ConvertSpeed>
#   </soapenv:Body>
#</soapenv:Envelope>”””

body = “””<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:f1=”http://yourcompany.com/F1-COUNTRY.xsd”&gt;
<soapenv:Header/>
<soapenv:Body>
<f1:F1-COUNTRY dateTimeTagFormat=”xsd:strict” transactionType=”READ”>
<f1:country>US</f1:country>
</f1:F1-COUNTRY>
</soapenv:Body>
</soapenv:Envelope>”””

#response = requests.post(url,data=body,headers=headers)
response = requests.post(url,data=body,headers=headers, auth=(‘user’, ‘pass’))  # no proxies  – used in intranet
#response = requests.post(url,data=body,headers=headers,proxies=proxies)  # used in extranet
#response = requests.post(url,data=body,headers=headers,proxies=proxies,auth=(‘user’, ‘pass’))

print(response.content)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s