Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Making a request with suds

Tags:

python

soap

suds

i'm testing out SUDS library and I'm trying to make a simple request to an endpoint but i get unusual output. Why?

from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)

url = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway"

client = Client(url)
print client

Output:

Martynass-MacBook-Air:CH martynas$ python ch.py
DEBUG:suds.xsd.schema:loaded:

schema collection
   Schema:0x109a7db90
   (raw)
      <schema/>
   (model)

DEBUG:suds.xsd.schema:MERGED:
Schema:0x109a7db90
(raw)
   <schema/>
(model)
like image 810
chuckfinley Avatar asked Dec 08 '13 00:12

chuckfinley


People also ask

How do I make a SOAP request in Python?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

What is suds in Python?

Suds is a SOAP module for Python, which allows Python to consume wsdl files. Setuptools that you installed in the previous step includes an Easy Install program that you can use to install the correct version of suds. Download the suds egg from https://pypi.python.org/pypi/suds.

Does Python support SOAP?

Python Web Services and Zolera Soap Infrastructure ((ZSI on PyPi) provides both client and server SOAP libraries.


1 Answers

You can't use suds for this servce, suds is based on SOAP, which is another web service protocol. What you can do is send an xml request and get a response.

import requests

target_url = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway"
headers={'Content-type': 'text/xml'}
print requests.post(target_url, data=xml, headers=headers).text

Where the xml is defined according to their schemes. http://xmlgw.companieshouse.gov.uk/example_http.html This is one examaple

xml = ('''
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader" 
xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 
xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader">
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>CompanyDetails</Class>
<Qualifier>request</Qualifier>
<TransactionID>14456553</TransactionID>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>My_SenderID</SenderID>
<Authentication>
<Method>CHMD5</Method>
<Value>e999e113407884fa410fa2f53bc23952</Value>
</Authentication>
</IDAuthentication>
<EmailAddress>[email protected]</EmailAddress>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys/>
</GovTalkDetails>
<Body>
<CompanyDetailsRequest xmlns="http://xmlgw.companieshouse.gov.uk/v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/CoDets.xsd">
<CompanyNumber>01002361</CompanyNumber>
<GiveMortTotals>1</GiveMortTotals>
</CompanyDetailsRequest>
</Body>
</GovTalkMessage>
''')

<Class>CompanyDetails</Class> What type of info you're getting. kinda what "function" to call

<Authentication>
<Method>CHMD5</Method>
<Value>e999e113407884fa410fa2f53bc23952</Value>
</Authentication>
</IDAuthentication>
Here you would put the login info i guess

<CompanyDetailsRequest xmlns="http://xmlgw.companieshouse.gov.uk/v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/CoDets.xsd"> <CompanyNumber>01002361</CompanyNumber> <GiveMortTotals>1</GiveMortTotals> </CompanyDetailsRequest> The "function" call and it's parameters

Now this will give me a response telling me the authorization failed. So if you have an account there, this should work for you.

Here you can find the list of schemes they have for different types of request. Some of them have sample request to help you out. http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/SchemaStatusOutput

Here is the complete guide of all their schemes. http://xmlgw.companieshouse.gov.uk/data_usage_guide_dec_2013.pdf

like image 54
M4rtini Avatar answered Oct 16 '22 17:10

M4rtini