Skip to content

Instantly share code, notes, and snippets.

@nenodias
Last active July 27, 2020 15:19
Show Gist options
  • Save nenodias/7ed727460675a4ed791b to your computer and use it in GitHub Desktop.
Save nenodias/7ed727460675a4ed791b to your computer and use it in GitHub Desktop.
Python Socket Soap Call
# *-* coding:utf-8 *-*
import os
import socket
target_host = "www.webservicex.com"
target_port = 80
target_path = "/globalweather.asmx"
target_namespace = "http://www.webserviceX.NET"
service = "GetCitiesByCountry"
xmldata = "";
xmldata += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">'
xmldata += '<soapenv:Header/>'
xmldata += '<soapenv:Body>'
xmldata += '<web:GetCitiesByCountry>'
xmldata += '<web:CountryName>Argentina</web:CountryName>'
xmldata += '</web:GetCitiesByCountry>'
xmldata += '</soapenv:Body>'
xmldata += '</soapenv:Envelope>'
tamanho = len(xmldata )
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect( (target_host, target_port) )
chamada = '''
POST http://{host}/{path} HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: "{namespace}/{service}"
Content-Length: {tamanho}
Host: {host}
Connection: Keep-Alive
{xml}
'''.format(host = target_host, path = target_path, namespace = target_namespace, service = service, xml = xmldata, tamanho = tamanho)
if bytes != str: # Testing if is python2 or python3
chamada = bytes(chamada, 'utf-8')
client.send(chamada)
response = client.recv(4096)
response = response.decode(encoding="utf-8")
response = response.replace("&lt;","<")
response = response.replace("&gt;",">")
print( response )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment