0

This is the code that I'm trying to use:

import os
import socket
import httplib

packet='''
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
 <soap12:Body>
  <GetCitiesByCountry xmlns="http://www.webserviceX.NET">
   <CountryName>India</CountryName>
  </GetCitiesByCountry>
 </soap12:Body>
</soap12:Envelope>'''
lent=len(packet)
msg="""
POST "www.webservicex.net/globalweather.asmx" HTTP/1.1
Host: www.webservicex.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: {length}
SOAPAction:"http://www.webserviceX.NET/GetCitiesByCountry"
Connection: Keep-Alive
{xml}""".format(length=lent,xml=packet)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect( ("www.webservicex.net",80) )
if bytes != str: # Testing if is python2 or python3
    msg = bytes(msg, 'utf-8')
client.send(msg)
client.settimeout(10)
print(client.type)
res=client.recv(4096)
print(res)
#res = res.replace("<","&lt;") -- only for stack overflow post
#res = res.replace(">","&gt;") -- only for stack overflow post

The output is:

HTTP/1.1 400 Bad Request                                                                                                                                        
Content-Type: text/html; charset=us-ascii                                                                                                                       
Server: Microsoft-HTTPAPI/2.0                                                                                                                                   
Date: Wed, 24 Aug 2016 11:40:02 GMT                                                                                                                             
Connection: close                                                                                                                                               
Content-Length: 324 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">;                                                                 
<HTML><HEAD><TITLE>Bad Request</TITLE>                                                                                                  
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>                                                                       
<BODY><h2>Bad Request - Invalid URL</h2>                                                                                                      
<hr><p>HTTP Error 400. The request URL is invalid.</p>                                                                                        
</BODY></HTML>

Any ideas?

Rakete1111
  • 47,013
  • 16
  • 123
  • 162

1 Answers1

0

You ought to use suds to consume SOAP web service.

EDIT: Example

import suds
import suds.transport
from suds.transport.http import HttpAuthenticated


class MyException(Exception):
    pass


service_url = "http://my/service/url"

GET the available services:

try:
    transport = HttpAuthenticated(username='elmer', password='fudd')
    wsdl = suds.client.Client(service_url, faults=False, transport=transport)

except IOError as exc:
    fmt = "Can initialize my service: {reason}"
    raise MyException(fmt.format(reason=exc))

except suds.transport.TransportError as exc:
    fmt = "HTTP error -- Is it a bad URL: {service_url}? {reason}"
    raise MyException(fmt.format(service_url=service_url, raison=exc))

Run a given service (here, its name is RunScript):

# add required options in headers
http_headers = {'sessionID': 1}
wsdl.set_options(soapheaders=http_headers)

params = {"scriptLanguage": "javascript", "scriptFile": "...",
          "scriptArgs": [{"name": "...", "value": "..."}]}

try:
    exit_code, response = wsdl.service.RunScript([params])
    # ...
except suds.MethodNotFound as reason:
    raise MyException("No method found: {reason}".format(reason=reason))
except suds.WebFault as reason:
    raise MyException("Error running the script: {reason}".format(reason=reason))
except Exception as reason:
    err_msg = "{0}".format(reason)
    if err_msg == "timed out":
        raise MyException("Timeout: {reason}".format(reason=reason))
    raise

Of course, it is not required to use an error manager here. But I give an example. The last with the "timed out" is a kind of hack I used to detect a time out in my application.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Thanks @Laurent but does suds work for any SOAP web service? – Ashutosh Sharma Aug 24 '16 at 12:30
  • It’s a general purpose SOAP client. I used it in production server to do page layout with [InDesign CC server](http://www.adobe.com/fr/products/indesignserver.html). It works well. – Laurent LAPORTE Aug 24 '16 at 12:35
  • Thank you so much Laurent I was able to invoke the function using this:) This worked as expected.. Two more questions for your sir .Thanx 1)In the client url I suppose its mandatory that url should be feeding WSDL response otherwise suds wont work? url="http://webservicex.net/globalweather.asmx" throws error 2)Anything on the auth to web service? from suds.client import Client url="http://webservicex.net/globalweather.asmx?wsdl" client = Client(url) print(client) print(client.service.GetCitiesByCountry("India")) – Ashutosh Sharma Aug 25 '16 at 03:03
  • It deserve a new question with appropriate tags and especially a code sample… 1) Yes you need a first query to retrieve the available services then a second one with the required service. See usage on my edit. 2) I don't know, what does the doc says about that? – Laurent LAPORTE Aug 25 '16 at 06:47
  • There is [HTTP authentication](https://fedorahosted.org/suds/wiki/Documentation#HTTPAUTHENTICATION): with login/password. – Laurent LAPORTE Aug 25 '16 at 07:05
  • Thanks sir for all the effort and help. i think if the ?wsdl is not defined for the web service that makes it private and suds can not access its because suds only reads WSDL to create client object and the only way to access that is the above code but that code has error :( – Ashutosh Sharma Aug 25 '16 at 07:26