initial commit
This commit is contained in:
commit
d421c1321b
580 files changed
+115527
No files matched your search
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"Contributed modules"
|
||||
|
||||
Executable
+689
@@ -0,0 +1,689 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: latin-1 -*-
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by the
|
||||
# Free Software Foundation; either version 3, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
|
||||
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
# for more details.
|
||||
|
||||
"Pythonic simple SOAP Client implementation"
|
||||
|
||||
__author__ = "Mariano Reingart (reingart@gmail.com)"
|
||||
__copyright__ = "Copyright (C) 2008 Mariano Reingart"
|
||||
__license__ = "LGPL 3.0"
|
||||
__version__ = "1.02c"
|
||||
|
||||
import urllib
|
||||
try:
|
||||
import httplib2
|
||||
Http = httplib2.Http
|
||||
except ImportError:
|
||||
import urllib2
|
||||
class Http(): # wrapper to use when httplib2 not available
|
||||
def request(self, url, method, body, headers):
|
||||
f = urllib2.urlopen(urllib2.Request(url, body, headers))
|
||||
return f.info(), f.read()
|
||||
|
||||
|
||||
from simplexml import SimpleXMLElement, TYPE_MAP, OrderedDict
|
||||
|
||||
class SoapFault(RuntimeError):
|
||||
def __init__(self,faultcode,faultstring):
|
||||
self.faultcode = faultcode
|
||||
self.faultstring = faultstring
|
||||
|
||||
# soap protocol specification & namespace
|
||||
soap_namespaces = dict(
|
||||
soap11="http://schemas.xmlsoap.org/soap/envelope/",
|
||||
soap="http://schemas.xmlsoap.org/soap/envelope/",
|
||||
soapenv="http://schemas.xmlsoap.org/soap/envelope/",
|
||||
soap12="http://www.w3.org/2003/05/soap-env",
|
||||
)
|
||||
|
||||
class SoapClient(object):
|
||||
"Simple SOAP Client (s�mil PHP)"
|
||||
def __init__(self, location = None, action = None, namespace = None,
|
||||
cert = None, trace = False, exceptions = True, proxy = None, ns=False,
|
||||
soap_ns=None, wsdl = None, cache = False):
|
||||
self.certssl = cert
|
||||
self.keyssl = None
|
||||
self.location = location # server location (url)
|
||||
self.action = action # SOAP base action
|
||||
self.namespace = namespace # message
|
||||
self.trace = trace # show debug messages
|
||||
self.exceptions = exceptions # lanzar execpiones? (Soap Faults)
|
||||
self.xml_request = self.xml_response = ''
|
||||
if not soap_ns and not ns:
|
||||
self.__soap_ns = 'soap' # 1.1
|
||||
elif not soap_ns and ns:
|
||||
self.__soap_ns = 'soapenv' # 1.2
|
||||
else:
|
||||
self.__soap_ns = soap_ns
|
||||
|
||||
# parse wsdl url
|
||||
self.services = wsdl and self.wsdl(wsdl, debug=trace, cache=cache)
|
||||
self.service_port = None # service port for late binding
|
||||
|
||||
if not proxy:
|
||||
self.http = Http()
|
||||
else:
|
||||
import socks
|
||||
##httplib2.debuglevel=4
|
||||
self.http = httplib2.Http(proxy_info = httplib2.ProxyInfo(
|
||||
proxy_type=socks.PROXY_TYPE_HTTP, **proxy))
|
||||
#if self.certssl: # esto funciona para validar al server?
|
||||
# self.http.add_certificate(self.keyssl, self.keyssl, self.certssl)
|
||||
self.__ns = ns # namespace prefix or False to not use it
|
||||
if not ns:
|
||||
self.__xml = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<%(soap_ns)s:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:%(soap_ns)s="%(soap_uri)s">
|
||||
<%(soap_ns)s:Body>
|
||||
<%(method)s xmlns="%(namespace)s">
|
||||
</%(method)s>
|
||||
</%(soap_ns)s:Body>
|
||||
</%(soap_ns)s:Envelope>"""
|
||||
else:
|
||||
self.__xml = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<%(soap_ns)s:Envelope xmlns:%(soap_ns)s="%(soap_uri)s" xmlns:%(ns)s="%(namespace)s">
|
||||
<%(soap_ns)s:Header/>
|
||||
<%(soap_ns)s:Body>
|
||||
<%(ns)s:%(method)s>
|
||||
</%(ns)s:%(method)s>
|
||||
</%(soap_ns)s:Body>
|
||||
</%(soap_ns)s:Envelope>"""
|
||||
|
||||
def __getattr__(self, attr):
|
||||
"Return a pseudo-method that can be called"
|
||||
if not self.services: # not using WSDL?
|
||||
return lambda self=self, *args, **kwargs: self.call(attr,*args,**kwargs)
|
||||
else: # using WSDL:
|
||||
return lambda self=self, *args, **kwargs: self.wsdl_call(attr,*args,**kwargs)
|
||||
|
||||
def call(self, method, *args, **kwargs):
|
||||
"Prepare xml request and make SOAP call, returning a SimpleXMLElement"
|
||||
#TODO: method != input_message
|
||||
# Basic SOAP request:
|
||||
xml = self.__xml % dict(method=method, namespace=self.namespace, ns=self.__ns,
|
||||
soap_ns=self.__soap_ns, soap_uri=soap_namespaces[self.__soap_ns])
|
||||
request = SimpleXMLElement(xml,namespace=self.__ns and self.namespace, prefix=self.__ns)
|
||||
# serialize parameters
|
||||
if kwargs:
|
||||
parameters = kwargs.items()
|
||||
else:
|
||||
parameters = args
|
||||
if parameters and isinstance(parameters[0], SimpleXMLElement):
|
||||
# merge xmlelement parameter ("raw" - already marshalled)
|
||||
for param in parameters[0].children():
|
||||
getattr(request,method).import_node(param)
|
||||
else:
|
||||
# marshall parameters:
|
||||
for k,v in parameters: # dict: tag=valor
|
||||
getattr(request,method).marshall(k,v)
|
||||
self.xml_request = request.as_xml()
|
||||
self.xml_response = self.send(method, self.xml_request)
|
||||
response = SimpleXMLElement(self.xml_response, namespace=self.namespace)
|
||||
if self.exceptions and response("Fault", ns=soap_namespaces.values(), error=False):
|
||||
raise SoapFault(unicode(response.faultcode), unicode(response.faultstring))
|
||||
return response
|
||||
|
||||
def send(self, method, xml):
|
||||
"Send SOAP request using HTTP"
|
||||
if self.location == 'test': return
|
||||
location = "%s" % self.location #?op=%s" % (self.location, method)
|
||||
if self.services:
|
||||
soap_action = self.action
|
||||
else:
|
||||
soap_action = self.action+method
|
||||
headers={
|
||||
'Content-type': 'text/xml; charset="UTF-8"',
|
||||
'Content-length': str(len(xml)),
|
||||
"SOAPAction": "\"%s\"" % (soap_action)
|
||||
}
|
||||
if self.trace:
|
||||
print "-"*80
|
||||
print "POST %s" % location
|
||||
print '\n'.join(["%s: %s" % (k,v) for k,v in headers.items()])
|
||||
print u"\n%s" % xml.decode("utf8","ignore")
|
||||
response, content = self.http.request(
|
||||
location,"POST", body=xml, headers=headers )
|
||||
self.response = response
|
||||
self.content = content
|
||||
if self.trace:
|
||||
print
|
||||
print '\n'.join(["%s: %s" % (k,v) for k,v in response.items()])
|
||||
print content#.decode("utf8","ignore")
|
||||
print "="*80
|
||||
return content
|
||||
|
||||
def get_operation(self, method):
|
||||
# try to find operation in wsdl file
|
||||
soap_ver = self.__soap_ns == 'soap12' and 'soap12' or 'soap11'
|
||||
if not self.service_port:
|
||||
for service_name, service in self.services.items():
|
||||
for port_name, port in [port for port in service['ports'].items()]:
|
||||
if port['soap_ver'] == soap_ver:
|
||||
self.service_port = service_name, port_name
|
||||
break
|
||||
else:
|
||||
raise RuntimeError("Cannot determine service in WSDL: "
|
||||
"SOAP version: %s" % soap_ver)
|
||||
else:
|
||||
port = self.services[self.service_port[0]]['ports'][self.service_port[1]]
|
||||
self.location = port['location']
|
||||
operation = port['operations'].get(unicode(method))
|
||||
if not operation:
|
||||
raise RuntimeError("Operation %s not found in WSDL: "
|
||||
"Service/Port Type: %s" %
|
||||
(method, self.service_port))
|
||||
return operation
|
||||
|
||||
def wsdl_call(self, method, *args, **kwargs):
|
||||
"Pre and post process SOAP call, input and output parameters using WSDL"
|
||||
soap_uri = soap_namespaces[self.__soap_ns]
|
||||
operation = self.get_operation(method)
|
||||
# get i/o type declarations:
|
||||
input = operation['input']
|
||||
output = operation['output']
|
||||
if 'action' in operation:
|
||||
self.action = operation['action']
|
||||
# sort parameters (same order as xsd:sequence)
|
||||
def sort_dict(od, d):
|
||||
if isinstance(od, dict):
|
||||
ret = OrderedDict()
|
||||
for k in od.keys():
|
||||
v = d.get(k)
|
||||
if v:
|
||||
if isinstance(v, dict):
|
||||
v = sort_dict(od[k], v)
|
||||
elif isinstance(v, list):
|
||||
v = [sort_dict(od[k][0], v1)
|
||||
for v1 in v]
|
||||
ret[str(k)] = v
|
||||
return ret
|
||||
else:
|
||||
return d
|
||||
if input and kwargs:
|
||||
params = sort_dict(input.values()[0], kwargs).items()
|
||||
method = input.keys()[0]
|
||||
#elif not input:
|
||||
#TODO: no message! (see wsmtxca.dummy)
|
||||
else:
|
||||
params = kwargs and kwargs.items()
|
||||
# call remote procedure
|
||||
response = self.call(method, *params)
|
||||
# parse results:
|
||||
resp = response('Body',ns=soap_uri).children().unmarshall(output)
|
||||
return resp and resp.values()[0] # pass Response tag children
|
||||
|
||||
def help(self, method):
|
||||
"Return operation documentation and invocation/returned value example"
|
||||
operation = self.get_operation(method)
|
||||
input = operation['input'].values()
|
||||
input = input and input[0]
|
||||
output = operation['output'].values()[0]
|
||||
return u"%s(%s)\n -> %s:\n\n%s" % (
|
||||
method,
|
||||
input and ", ".join("%s=%s" % (k,repr(v)) for k,v
|
||||
in input.items()) or "",
|
||||
output and output or "",
|
||||
operation.get("documentation",""),
|
||||
)
|
||||
|
||||
def wsdl(self, url, debug=False, cache=False):
|
||||
"Parse Web Service Description v1.1"
|
||||
soap_ns = {
|
||||
"http://schemas.xmlsoap.org/wsdl/soap/": 'soap11',
|
||||
"http://schemas.xmlsoap.org/wsdl/soap12/": 'soap12',
|
||||
}
|
||||
wsdl_uri="http://schemas.xmlsoap.org/wsdl/"
|
||||
xsd_uri="http://www.w3.org/2001/XMLSchema"
|
||||
xsi_uri="http://www.w3.org/2001/XMLSchema-instance"
|
||||
|
||||
get_local_name = lambda s: str((':' in s) and s.split(':')[1] or s)
|
||||
|
||||
REVERSE_TYPE_MAP = dict([(v,k) for k,v in TYPE_MAP.items()])
|
||||
|
||||
def fetch(url):
|
||||
"Fetch a document from a URL, save it locally if cache enabled"
|
||||
import os, hashlib
|
||||
# make md5 hash of the url for caching...
|
||||
filename = "%s.xml" % hashlib.md5(url).hexdigest()
|
||||
if isinstance(cache, basestring):
|
||||
filename = os.path.join(cache, filename)
|
||||
if cache and os.path.exists(filename):
|
||||
if debug: print "Reading file %s" % (filename, )
|
||||
f = open(filename, "r")
|
||||
xml = f.read()
|
||||
f.close()
|
||||
else:
|
||||
if debug: print "Fetching url %s" % (url, )
|
||||
f = urllib.urlopen(url)
|
||||
xml = f.read()
|
||||
if cache:
|
||||
if debug: print "Writing file %s" % (filename, )
|
||||
f = open(filename, "w")
|
||||
f.write(xml)
|
||||
f.close()
|
||||
return xml
|
||||
|
||||
# Open uri and read xml:
|
||||
xml = fetch(url)
|
||||
# Parse WSDL XML:
|
||||
wsdl = SimpleXMLElement(xml, namespace=wsdl_uri)
|
||||
|
||||
# detect soap prefix and uri (xmlns attributes of <definitions>)
|
||||
xsd_ns = None
|
||||
soap_uris = {}
|
||||
for k, v in wsdl[:]:
|
||||
if v in soap_ns and k.startswith("xmlns:"):
|
||||
soap_uris[get_local_name(k)] = v
|
||||
if v== xsd_uri and k.startswith("xmlns:"):
|
||||
xsd_ns = get_local_name(k)
|
||||
|
||||
# Extract useful data:
|
||||
self.namespace = wsdl['targetNamespace']
|
||||
self.documentation = unicode(wsdl('documentation', error=False) or '')
|
||||
|
||||
services = {}
|
||||
bindings = {} # binding_name: binding
|
||||
operations = {} # operation_name: operation
|
||||
port_type_bindings = {} # port_type_name: binding
|
||||
messages = {} # message: element
|
||||
elements = {} # element: type def
|
||||
|
||||
for service in wsdl.service:
|
||||
service_name=service['name']
|
||||
if not service_name:
|
||||
continue # empty service?
|
||||
if debug: print "Processing service", service_name
|
||||
serv = services.setdefault(service_name, {'ports': {}})
|
||||
serv['documentation']=service['documentation'] or ''
|
||||
for port in service.port:
|
||||
binding_name = get_local_name(port['binding'])
|
||||
address = port('address', ns=soap_uris.values(), error=False)
|
||||
location = address and address['location'] or None
|
||||
soap_uri = address and soap_uris.get(address.get_prefix())
|
||||
soap_ver = soap_uri and soap_ns.get(soap_uri)
|
||||
bindings[binding_name] = {'service_name': service_name,
|
||||
'location': location,
|
||||
'soap_uri': soap_uri, 'soap_ver': soap_ver,
|
||||
}
|
||||
serv['ports'][port['name']] = bindings[binding_name]
|
||||
|
||||
for binding in wsdl.binding:
|
||||
binding_name = binding['name']
|
||||
if debug: print "Processing binding", service_name
|
||||
soap_binding = binding('binding', ns=soap_uris.values(), error=False)
|
||||
transport = soap_binding and soap_binding['transport'] or None
|
||||
port_type_name = get_local_name(binding['type'])
|
||||
bindings[binding_name].update({
|
||||
'port_type_name': port_type_name,
|
||||
'transport': transport, 'operations': {},
|
||||
})
|
||||
port_type_bindings[port_type_name] = bindings[binding_name]
|
||||
for operation in binding.operation:
|
||||
op_name = operation['name']
|
||||
op = operation('operation',ns=soap_uris.values(), error=False)
|
||||
action = op and op['soapAction']
|
||||
d = operations.setdefault(op_name, {})
|
||||
bindings[binding_name]['operations'][op_name] = d
|
||||
d.update({'name': op_name})
|
||||
#if action: #TODO: separe operation_binding from operation
|
||||
if action:
|
||||
d["action"] = action
|
||||
|
||||
#TODO: cleanup element/schema/types parsing:
|
||||
def process_element(element_name, node):
|
||||
"Parse and define simple element types"
|
||||
if debug: print "Processing element", element_name
|
||||
for tag in node:
|
||||
if tag.get_local_name() in ("annotation", "documentation"):
|
||||
continue
|
||||
elif tag.get_local_name() in ('element', 'restriction'):
|
||||
if debug: print element_name,"has not children!",tag
|
||||
children = tag # element "alias"?
|
||||
alias = True
|
||||
elif tag.children():
|
||||
children = tag.children()
|
||||
alias = False
|
||||
else:
|
||||
if debug: print element_name,"has not children!",tag
|
||||
continue #TODO: abstract?
|
||||
d = OrderedDict()
|
||||
for e in children:
|
||||
t = e['type']
|
||||
if not t:
|
||||
t = e['base'] # complexContent (extension)!
|
||||
if not t:
|
||||
t = 'anyType' # no type given!
|
||||
t = t.split(":")
|
||||
if len(t)>1:
|
||||
ns, type_name = t
|
||||
else:
|
||||
ns, type_name = None, t[0]
|
||||
if element_name == type_name:
|
||||
continue # prevent infinite recursion
|
||||
uri = ns and e.get_namespace_uri(ns) or xsd_uri
|
||||
if uri==xsd_uri:
|
||||
# look for the type, None == any
|
||||
fn = REVERSE_TYPE_MAP.get(unicode(type_name), None)
|
||||
else:
|
||||
# complex type, postprocess later
|
||||
fn = elements.setdefault(unicode(type_name), OrderedDict())
|
||||
if e['name'] is not None and not alias:
|
||||
e_name = unicode(e['name'])
|
||||
d[e_name] = fn
|
||||
else:
|
||||
if debug: print "complexConent/simpleType/element", element_name, "=", type_name
|
||||
d[None] = fn
|
||||
if e['maxOccurs']=="unbounded":
|
||||
# it's an array... TODO: compound arrays?
|
||||
d.array = True
|
||||
if e is not None and e.get_local_name() == 'extension' and e.children():
|
||||
# extend base element:
|
||||
process_element(element_name, e.children())
|
||||
elements.setdefault(element_name, OrderedDict()).update(d)
|
||||
|
||||
# check axis2 namespace at schema types attributes
|
||||
self.namespace = dict(wsdl.types("schema", ns=xsd_uri)[:]).get('targetNamespace', self.namespace)
|
||||
|
||||
imported_schemas = {}
|
||||
|
||||
def preprocess_schema(schema):
|
||||
"Find schema elements and complex types"
|
||||
for element in schema.children():
|
||||
if element.get_local_name() in ('import', ):
|
||||
schema_namespace = element['namespace']
|
||||
schema_location = element['schemaLocation']
|
||||
if schema_location is None:
|
||||
if debug: print "Schema location not provided for %s!" % (schema_namespace, )
|
||||
continue
|
||||
if schema_location in imported_schemas:
|
||||
if debug: print "Schema %s already imported!" % (schema_location, )
|
||||
continue
|
||||
imported_schemas[schema_location] = schema_namespace
|
||||
if debug: print "Importing schema %s from %s" % (schema_namespace, schema_location)
|
||||
# Open uri and read xml:
|
||||
xml = fetch(schema_location)
|
||||
# Parse imported XML schema (recursively):
|
||||
imported_schema = SimpleXMLElement(xml, namespace=xsd_uri)
|
||||
preprocess_schema(imported_schema)
|
||||
|
||||
if element.get_local_name() in ('element', 'complexType', "simpleType"):
|
||||
element_name = unicode(element['name'])
|
||||
if debug: print "Parsing Element %s: %s" % (element.get_local_name(),element_name)
|
||||
if element.get_local_name() == 'complexType':
|
||||
children = element.children()
|
||||
elif element.get_local_name() == 'simpleType':
|
||||
children = element("restriction", ns=xsd_uri)
|
||||
elif element.get_local_name() == 'element' and element['type']:
|
||||
children = element
|
||||
else:
|
||||
children = element.children()
|
||||
if children:
|
||||
children = children.children()
|
||||
elif element.get_local_name() == 'element':
|
||||
children = element
|
||||
if children:
|
||||
process_element(element_name, children)
|
||||
|
||||
def postprocess_element(elements):
|
||||
"Fix unresolved references (elements referenced before its definition, thanks .net)"
|
||||
for k,v in elements.items():
|
||||
if isinstance(v, OrderedDict):
|
||||
if v.array:
|
||||
elements[k] = [v] # convert arrays to python lists
|
||||
if v!=elements: #TODO: fix recursive elements
|
||||
postprocess_element(v)
|
||||
if None in v and v[None]: # extension base?
|
||||
if isinstance(v[None], dict):
|
||||
for i, kk in enumerate(v[None]):
|
||||
# extend base -keep orginal order-
|
||||
elements[k].insert(kk, v[None][kk], i)
|
||||
del v[None]
|
||||
else: # "alias", just replace
|
||||
if debug: print "Replacing ", k , " = ", v[None]
|
||||
elements[k] = v[None]
|
||||
#break
|
||||
if isinstance(v, list):
|
||||
for n in v: # recurse list
|
||||
postprocess_element(n)
|
||||
|
||||
|
||||
# process current wsdl schema:
|
||||
for schema in wsdl.types("schema", ns=xsd_uri):
|
||||
preprocess_schema(schema)
|
||||
|
||||
postprocess_element(elements)
|
||||
|
||||
for message in wsdl.message:
|
||||
if debug: print "Processing message", message['name']
|
||||
part = message('part', error=False)
|
||||
element = {}
|
||||
if part:
|
||||
element_name = part['element']
|
||||
if not element_name:
|
||||
element_name = part['type'] # some uses type instead
|
||||
element_name = get_local_name(element_name)
|
||||
element = {element_name: elements.get(element_name)}
|
||||
messages[message['name']] = element
|
||||
|
||||
for port_type in wsdl.portType:
|
||||
port_type_name = port_type['name']
|
||||
if debug: print "Processing port type", port_type_name
|
||||
binding = port_type_bindings[port_type_name]
|
||||
|
||||
for operation in port_type.operation:
|
||||
op_name = operation['name']
|
||||
op = operations[op_name]
|
||||
op['documentation'] = unicode(operation('documentation', error=False) or '')
|
||||
if binding['soap_ver']:
|
||||
#TODO: separe operation_binding from operation (non SOAP?)
|
||||
input = get_local_name(operation.input['message'])
|
||||
output = get_local_name(operation.output['message'])
|
||||
op['input'] = messages[input]
|
||||
op['output'] = messages[output]
|
||||
|
||||
if debug:
|
||||
import pprint
|
||||
pprint.pprint(services)
|
||||
|
||||
return services
|
||||
|
||||
def parse_proxy(proxy_str):
|
||||
"Parses proxy address user:pass@host:port into a dict suitable for httplib2"
|
||||
proxy_dict = {}
|
||||
if proxy_str is None:
|
||||
return
|
||||
if "@" in proxy_str:
|
||||
user_pass, host_port = proxy_str.split("@")
|
||||
else:
|
||||
user_pass, host_port = "", proxy_str
|
||||
if ":" in host_port:
|
||||
host, port = host_port.split(":")
|
||||
proxy_dict['proxy_host'], proxy_dict['proxy_port'] = host, int(port)
|
||||
if ":" in user_pass:
|
||||
proxy_dict['proxy_user'], proxy_dict['proxy_pass'] = user_pass.split(":")
|
||||
return proxy_dict
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
import sys
|
||||
|
||||
if '--web2py' in sys.argv:
|
||||
# test local sample webservice exposed by web2py
|
||||
from client import SoapClient
|
||||
if not '--wsdl' in sys.argv:
|
||||
client = SoapClient(
|
||||
location = "http://127.0.0.1:8000/webservices/sample/call/soap",
|
||||
action = 'http://127.0.0.1:8000/webservices/sample/call/soap', # SOAPAction
|
||||
namespace = "http://127.0.0.1:8000/webservices/sample/call/soap",
|
||||
soap_ns='soap', trace = True, ns = False, exceptions=True)
|
||||
else:
|
||||
client = SoapClient(wsdl="http://127.0.0.1:8000/webservices/sample/call/soap?WSDL",trace=True)
|
||||
response = client.Dummy()
|
||||
print 'dummy', response
|
||||
response = client.Echo(value='hola')
|
||||
print 'echo', repr(response)
|
||||
response = client.AddIntegers(a=1,b=2)
|
||||
if not '--wsdl' in sys.argv:
|
||||
result = response.AddResult # manully convert returned type
|
||||
print int(result)
|
||||
else:
|
||||
result = response['AddResult']
|
||||
print result, type(result), "auto-unmarshalled"
|
||||
|
||||
if '--raw' in sys.argv:
|
||||
# raw (unmarshalled parameter) local sample webservice exposed by web2py
|
||||
from client import SoapClient
|
||||
client = SoapClient(
|
||||
location = "http://127.0.0.1:8000/webservices/sample/call/soap",
|
||||
action = 'http://127.0.0.1:8000/webservices/sample/call/soap', # SOAPAction
|
||||
namespace = "http://127.0.0.1:8000/webservices/sample/call/soap",
|
||||
soap_ns='soap', trace = True, ns = False)
|
||||
params = SimpleXMLElement("""<?xml version="1.0" encoding="UTF-8"?><AddIntegers><a>3</a><b>2</b></AddIntegers>""") # manully convert returned type
|
||||
response = client.call('AddIntegers',params)
|
||||
result = response.AddResult
|
||||
print int(result) # manully convert returned type
|
||||
|
||||
if '--ctg' in sys.argv:
|
||||
# test AFIP Agriculture webservice
|
||||
client = SoapClient(
|
||||
location = "https://fwshomo.afip.gov.ar/wsctg/services/CTGService",
|
||||
action = 'http://impl.service.wsctg.afip.gov.ar/CTGService/', # SOAPAction
|
||||
namespace = "http://impl.service.wsctg.afip.gov.ar/CTGService/",
|
||||
trace = True,
|
||||
ns = True)
|
||||
response = client.dummy()
|
||||
result = response.dummyResponse
|
||||
print str(result.appserver)
|
||||
print str(result.dbserver)
|
||||
print str(result.authserver)
|
||||
|
||||
if '--wsfe' in sys.argv:
|
||||
# Demo & Test (AFIP Electronic Invoice):
|
||||
ta_file = open("TA.xml")
|
||||
try:
|
||||
ta_string = ta_file.read() # read access ticket (wsaa.py)
|
||||
finally:
|
||||
ta_file.close()
|
||||
ta = SimpleXMLElement(ta_string)
|
||||
token = str(ta.credentials.token)
|
||||
sign = str(ta.credentials.sign)
|
||||
cuit = long(20267565393)
|
||||
id = 1234
|
||||
cbte =199
|
||||
client = SoapClient(
|
||||
location = "https://wswhomo.afip.gov.ar/wsfe/service.asmx",
|
||||
action = 'http://ar.gov.afip.dif.facturaelectronica/', # SOAPAction
|
||||
namespace = "http://ar.gov.afip.dif.facturaelectronica/",
|
||||
trace = True)
|
||||
results = client.FERecuperaQTYRequest(
|
||||
argAuth= {"Token": token, "Sign": sign, "cuit":long(cuit)}
|
||||
)
|
||||
if int(results.FERecuperaQTYRequestResult.RError.percode) != 0:
|
||||
print "Percode: %s" % results.FERecuperaQTYRequestResult.RError.percode
|
||||
print "MSGerror: %s" % results.FERecuperaQTYRequestResult.RError.perrmsg
|
||||
else:
|
||||
print int(results.FERecuperaQTYRequestResult.qty.value)
|
||||
|
||||
if '--feriados' in sys.argv:
|
||||
# Demo & Test: Argentina Holidays (Ministerio del Interior):
|
||||
# this webservice seems disabled
|
||||
from datetime import datetime, timedelta
|
||||
client = SoapClient(
|
||||
location = "http://webservices.mininterior.gov.ar/Feriados/Service.svc",
|
||||
action = 'http://tempuri.org/IMyService/', # SOAPAction
|
||||
namespace = "http://tempuri.org/FeriadoDS.xsd",
|
||||
trace = True)
|
||||
dt1 = datetime.today() - timedelta(days=60)
|
||||
dt2 = datetime.today() + timedelta(days=60)
|
||||
feriadosXML = client.FeriadosEntreFechasas_xml(dt1=dt1.isoformat(), dt2=dt2.isoformat());
|
||||
print feriadosXML
|
||||
|
||||
if '--wsdl-parse' in sys.argv:
|
||||
client = SoapClient()
|
||||
# Test PySimpleSOAP WSDL
|
||||
client.wsdl("file:C:/test.wsdl", debug=True)
|
||||
# Test Java Axis WSDL:
|
||||
client.wsdl('https://wsaahomo.afip.gov.ar/ws/services/LoginCms?wsdl',debug=True)
|
||||
# Test .NET 2.0 WSDL:
|
||||
client.wsdl('https://wswhomo.afip.gov.ar/wsfe/service.asmx?WSDL',debug=True)
|
||||
client.wsdl('https://wswhomo.afip.gov.ar/wsfex/service.asmx?WSDL',debug=True)
|
||||
client.wsdl('https://testdia.afip.gov.ar/Dia/Ws/wDigDepFiel/wDigDepFiel.asmx?WSDL',debug=True)
|
||||
# Test JBoss WSDL:
|
||||
client.wsdl('https://fwshomo.afip.gov.ar/wsctg/services/CTGService?wsdl',debug=True)
|
||||
client.wsdl('https://wsaahomo.afip.gov.ar/ws/services/LoginCms?wsdl',debug=True)
|
||||
|
||||
if '--wsdl-client' in sys.argv:
|
||||
client = SoapClient(wsdl='https://wswhomo.afip.gov.ar/wsfex/service.asmx?WSDL',trace=True)
|
||||
results = client.FEXDummy()
|
||||
print results['FEXDummyResult']['AppServer']
|
||||
print results['FEXDummyResult']['DbServer']
|
||||
print results['FEXDummyResult']['AuthServer']
|
||||
ta_file = open("TA.xml")
|
||||
try:
|
||||
ta_string = ta_file.read() # read access ticket (wsaa.py)
|
||||
finally:
|
||||
ta_file.close()
|
||||
ta = SimpleXMLElement(ta_string)
|
||||
token = str(ta.credentials.token)
|
||||
sign = str(ta.credentials.sign)
|
||||
response = client.FEXGetCMP(
|
||||
Auth={"Token": token, "Sign": sign, "Cuit": 20267565393},
|
||||
Cmp={"Tipo_cbte": 19, "Punto_vta": 1, "Cbte_nro": 1})
|
||||
result = response['FEXGetCMPResult']
|
||||
if False: print result
|
||||
if 'FEXErr' in result:
|
||||
print "FEXError:", result['FEXErr']['ErrCode'], result['FEXErr']['ErrCode']
|
||||
cbt = result['FEXResultGet']
|
||||
print cbt['Cae']
|
||||
FEX_event = result['FEXEvents']
|
||||
print FEX_event['EventCode'], FEX_event['EventMsg']
|
||||
|
||||
if '--wsdl-ctg' in sys.argv:
|
||||
client = SoapClient(wsdl='https://fwshomo.afip.gov.ar/wsctg/services/CTGService?wsdl',
|
||||
trace=True, ns = "ctg")
|
||||
results = client.dummy()
|
||||
print results
|
||||
print results['DummyResponse']['appserver']
|
||||
print results['DummyResponse']['dbserver']
|
||||
print results['DummyResponse']['authserver']
|
||||
ta_file = open("TA.xml")
|
||||
try:
|
||||
ta_string = ta_file.read() # read access ticket (wsaa.py)
|
||||
finally:
|
||||
ta_file.close()
|
||||
ta = SimpleXMLElement(ta_string)
|
||||
token = str(ta.credentials.token)
|
||||
sign = str(ta.credentials.sign)
|
||||
print client.help("obtenerProvincias")
|
||||
response = client.obtenerProvincias(auth={"token":token, "sign":sign, "cuitRepresentado":20267565393})
|
||||
print "response=",response
|
||||
for ret in response:
|
||||
print ret['return']['codigoProvincia'], ret['return']['descripcionProvincia'].encode("latin1")
|
||||
prueba = dict(numeroCartaDePorte=512345678, codigoEspecie=23,
|
||||
cuitRemitenteComercial=20267565393, cuitDestino=20267565393, cuitDestinatario=20267565393,
|
||||
codigoLocalidadOrigen=3058, codigoLocalidadDestino=3059,
|
||||
codigoCosecha='0910', pesoNetoCarga=1000, cantHoras=1,
|
||||
patenteVehiculo='CZO985', cuitTransportista=20267565393,
|
||||
numeroCTG="43816783", transaccion='10000001681', observaciones='',
|
||||
)
|
||||
|
||||
response = client.solicitarCTG(
|
||||
auth={"token": token, "sign": sign, "cuitRepresentado": 20267565393},
|
||||
solicitarCTGRequest= prueba)
|
||||
|
||||
print response['return']['numeroCTG']
|
||||
|
||||
##print parse_proxy(None)
|
||||
##print parse_proxy("host:1234")
|
||||
##print parse_proxy("user:pass@host:1234")
|
||||
##sys.exit(0)
|
||||
|
||||
Executable
+455
@@ -0,0 +1,455 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: latin-1 -*-
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by the
|
||||
# Free Software Foundation; either version 3, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
|
||||
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
# for more details.
|
||||
|
||||
"Simple SOAP Server implementation"
|
||||
|
||||
__author__ = "Mariano Reingart (reingart@gmail.com)"
|
||||
__copyright__ = "Copyright (C) 2010 Mariano Reingart"
|
||||
__license__ = "LGPL 3.0"
|
||||
__version__ = "1.02c"
|
||||
|
||||
from simplexml import SimpleXMLElement, TYPE_MAP, DateTime, Date, Decimal
|
||||
|
||||
DEBUG = False
|
||||
|
||||
|
||||
class SoapDispatcher(object):
|
||||
"Simple Dispatcher for SOAP Server"
|
||||
|
||||
def __init__(self, name, documentation='', action='', location='',
|
||||
namespace=None, prefix=False,
|
||||
soap_uri="http://schemas.xmlsoap.org/soap/envelope/",
|
||||
soap_ns='soap',
|
||||
**kwargs):
|
||||
self.methods = {}
|
||||
self.name = name
|
||||
self.documentation = documentation
|
||||
self.action = action # base SoapAction
|
||||
self.location = location
|
||||
self.namespace = namespace # targetNamespace
|
||||
self.prefix = prefix
|
||||
self.soap_ns = soap_ns
|
||||
self.soap_uri = soap_uri
|
||||
|
||||
def register_function(self, name, fn, returns=None, args=None, doc=None):
|
||||
self.methods[name] = fn, returns, args, doc or getattr(fn,"__doc__","")
|
||||
|
||||
def dispatch(self, xml, action=None):
|
||||
"Receive and proccess SOAP call"
|
||||
# default values:
|
||||
prefix = self.prefix
|
||||
ret = fault = None
|
||||
soap_ns, soap_uri = self.soap_ns, self.soap_uri
|
||||
soap_fault_code = 'VersionMismatch'
|
||||
|
||||
try:
|
||||
request = SimpleXMLElement(xml, namespace=self.namespace)
|
||||
|
||||
# detect soap prefix and uri (xmlns attributes of Envelope)
|
||||
for k, v in request[:]:
|
||||
if v in ("http://schemas.xmlsoap.org/soap/envelope/",
|
||||
"http://www.w3.org/2003/05/soap-env",):
|
||||
soap_ns = request.attributes()[k].localName
|
||||
soap_uri = request.attributes()[k].value
|
||||
|
||||
soap_fault_code = 'Client'
|
||||
|
||||
# parse request message and get local method
|
||||
method = request('Body', ns=soap_uri).children()(0)
|
||||
if action:
|
||||
# method name = action
|
||||
name = action[len(self.action)+1:-1]
|
||||
prefix = self.prefix
|
||||
if not action or not name:
|
||||
# method name = input message name
|
||||
name = method.get_local_name()
|
||||
prefix = method.get_prefix()
|
||||
|
||||
if DEBUG: print "dispatch method", name
|
||||
function, returns_types, args_types, doc = self.methods[name]
|
||||
|
||||
# de-serialize parameters (if type definitions given)
|
||||
if args_types:
|
||||
args = method.children().unmarshall(args_types)
|
||||
elif args_types is None:
|
||||
args = {'request':method} # send raw request
|
||||
else:
|
||||
args = {} # no parameters
|
||||
|
||||
soap_fault_code = 'Server'
|
||||
# execute function
|
||||
ret = function(**args)
|
||||
if DEBUG: print ret
|
||||
|
||||
except Exception, e:
|
||||
import sys
|
||||
etype, evalue, etb = sys.exc_info()
|
||||
if DEBUG:
|
||||
import traceback
|
||||
detail = ''.join(traceback.format_exception(etype, evalue, etb))
|
||||
detail += '\n\nXML REQUEST\n\n' + xml
|
||||
else:
|
||||
detail = None
|
||||
fault = {'faultcode': "%s.%s" % (soap_fault_code, etype.__name__),
|
||||
'faultstring': unicode(evalue),
|
||||
'detail': detail}
|
||||
|
||||
# build response message
|
||||
if not prefix:
|
||||
xml = """<%(soap_ns)s:Envelope xmlns:%(soap_ns)s="%(soap_uri)s"/>"""
|
||||
else:
|
||||
xml = """<%(soap_ns)s:Envelope xmlns:%(soap_ns)s="%(soap_uri)s"
|
||||
xmlns:%(prefix)s="%(namespace)s"/>"""
|
||||
|
||||
xml = xml % {'namespace': self.namespace, 'prefix': prefix,
|
||||
'soap_ns': soap_ns, 'soap_uri': soap_uri}
|
||||
|
||||
response = SimpleXMLElement(xml, namespace=self.namespace,
|
||||
prefix=prefix)
|
||||
|
||||
response['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
|
||||
response['xmlns:xsd'] = "http://www.w3.org/2001/XMLSchema"
|
||||
|
||||
body = response.add_child("%s:Body" % soap_ns, ns=False)
|
||||
if fault:
|
||||
# generate a Soap Fault (with the python exception)
|
||||
body.marshall("%s:Fault" % soap_ns, fault, ns=False)
|
||||
else:
|
||||
# return normal value
|
||||
res = body.add_child("%sResponse" % name, ns=prefix)
|
||||
if not prefix:
|
||||
res['xmlns'] = self.namespace # add target namespace
|
||||
|
||||
# serialize returned values (response) if type definition available
|
||||
if returns_types:
|
||||
if not isinstance(ret, dict):
|
||||
res.marshall(returns_types.keys()[0], ret, )
|
||||
else:
|
||||
for k,v in ret.items():
|
||||
res.marshall(k, v)
|
||||
elif returns_types is None:
|
||||
# merge xmlelement returned
|
||||
res.import_node(ret)
|
||||
|
||||
return response.as_xml()
|
||||
|
||||
# Introspection functions:
|
||||
|
||||
def list_methods(self):
|
||||
"Return a list of aregistered operations"
|
||||
return [(method, doc) for method, (function, returns, args, doc) in self.methods.items()]
|
||||
|
||||
def help(self, method=None):
|
||||
"Generate sample request and response messages"
|
||||
(function, returns, args, doc) = self.methods[method]
|
||||
xml = """
|
||||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<soap:Body><%(method)s xmlns="%(namespace)s"/></soap:Body>
|
||||
</soap:Envelope>""" % {'method':method, 'namespace':self.namespace}
|
||||
request = SimpleXMLElement(xml, namespace=self.namespace, prefix=self.prefix)
|
||||
if args:
|
||||
items = args.items()
|
||||
elif args is None:
|
||||
items = [('value', None)]
|
||||
else:
|
||||
items = []
|
||||
for k,v in items:
|
||||
request(method).marshall(k, v, add_comments=True, ns=False)
|
||||
|
||||
xml = """
|
||||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<soap:Body><%(method)sResponse xmlns="%(namespace)s"/></soap:Body>
|
||||
</soap:Envelope>""" % {'method':method, 'namespace':self.namespace}
|
||||
response = SimpleXMLElement(xml, namespace=self.namespace, prefix=self.prefix)
|
||||
if returns:
|
||||
items = returns.items()
|
||||
elif args is None:
|
||||
items = [('value', None)]
|
||||
else:
|
||||
items = []
|
||||
for k,v in items:
|
||||
response('%sResponse'%method).marshall(k, v, add_comments=True, ns=False)
|
||||
|
||||
return request.as_xml(pretty=True), response.as_xml(pretty=True), doc
|
||||
|
||||
|
||||
def wsdl(self):
|
||||
"Generate Web Service Description v1.1"
|
||||
xml = """<?xml version="1.0"?>
|
||||
<wsdl:definitions name="%(name)s"
|
||||
targetNamespace="%(namespace)s"
|
||||
xmlns:tns="%(namespace)s"
|
||||
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
|
||||
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">%(documentation)s</wsdl:documentation>
|
||||
|
||||
<wsdl:types>
|
||||
<xsd:schema targetNamespace="%(namespace)s"
|
||||
elementFormDefault="qualified"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
</xsd:schema>
|
||||
</wsdl:types>
|
||||
|
||||
</wsdl:definitions>
|
||||
""" % {'namespace': self.namespace, 'name': self.name, 'documentation': self.documentation}
|
||||
wsdl = SimpleXMLElement(xml)
|
||||
|
||||
for method, (function, returns, args, doc) in self.methods.items():
|
||||
# create elements:
|
||||
|
||||
def parse_element(name, values, array=False, complex=False):
|
||||
if not complex:
|
||||
element = wsdl('wsdl:types')('xsd:schema').add_child('xsd:element')
|
||||
complex = element.add_child("xsd:complexType")
|
||||
else:
|
||||
complex = wsdl('wsdl:types')('xsd:schema').add_child('xsd:complexType')
|
||||
element = complex
|
||||
element['name'] = name
|
||||
if values:
|
||||
items = values
|
||||
elif values is None:
|
||||
items = [('value', None)]
|
||||
else:
|
||||
items = []
|
||||
if not array and items:
|
||||
all = complex.add_child("xsd:all")
|
||||
elif items:
|
||||
all = complex.add_child("xsd:sequence")
|
||||
for k,v in items:
|
||||
e = all.add_child("xsd:element")
|
||||
e['name'] = k
|
||||
if array:
|
||||
e[:]={'minOccurs': "0", 'maxOccurs': "unbounded"}
|
||||
if v in TYPE_MAP.keys():
|
||||
t='xsd:%s' % TYPE_MAP[v]
|
||||
elif v is None:
|
||||
t='xsd:anyType'
|
||||
elif isinstance(v, list):
|
||||
n="ArrayOf%s%s" % (name, k)
|
||||
l = []
|
||||
for d in v:
|
||||
l.extend(d.items())
|
||||
parse_element(n, l, array=True, complex=True)
|
||||
t = "tns:%s" % n
|
||||
elif isinstance(v, dict):
|
||||
n="%s%s" % (name, k)
|
||||
parse_element(n, v.items(), complex=True)
|
||||
t = "tns:%s" % n
|
||||
e.add_attribute('type', t)
|
||||
|
||||
parse_element("%s" % method, args and args.items())
|
||||
parse_element("%sResponse" % method, returns and returns.items())
|
||||
|
||||
# create messages:
|
||||
for m,e in ('Input',''), ('Output','Response'):
|
||||
message = wsdl.add_child('wsdl:message')
|
||||
message['name'] = "%s%s" % (method, m)
|
||||
part = message.add_child("wsdl:part")
|
||||
part[:] = {'name': 'parameters',
|
||||
'element': 'tns:%s%s' % (method,e)}
|
||||
|
||||
# create ports
|
||||
portType = wsdl.add_child('wsdl:portType')
|
||||
portType['name'] = "%sPortType" % self.name
|
||||
for method, (function, returns, args, doc) in self.methods.items():
|
||||
op = portType.add_child('wsdl:operation')
|
||||
op['name'] = method
|
||||
if doc:
|
||||
op.add_child("wsdl:documentation", doc)
|
||||
input = op.add_child("wsdl:input")
|
||||
input['message'] = "tns:%sInput" % method
|
||||
output = op.add_child("wsdl:output")
|
||||
output['message'] = "tns:%sOutput" % method
|
||||
|
||||
# create bindings
|
||||
binding = wsdl.add_child('wsdl:binding')
|
||||
binding['name'] = "%sBinding" % self.name
|
||||
binding['type'] = "tns:%sPortType" % self.name
|
||||
soapbinding = binding.add_child('soap:binding')
|
||||
soapbinding['style'] = "document"
|
||||
soapbinding['transport'] = "http://schemas.xmlsoap.org/soap/http"
|
||||
for method in self.methods.keys():
|
||||
op = binding.add_child('wsdl:operation')
|
||||
op['name'] = method
|
||||
soapop = op.add_child('soap:operation')
|
||||
soapop['soapAction'] = self.action + method
|
||||
soapop['style'] = 'document'
|
||||
input = op.add_child("wsdl:input")
|
||||
##input.add_attribute('name', "%sInput" % method)
|
||||
soapbody = input.add_child("soap:body")
|
||||
soapbody["use"] = "literal"
|
||||
output = op.add_child("wsdl:output")
|
||||
##output.add_attribute('name', "%sOutput" % method)
|
||||
soapbody = output.add_child("soap:body")
|
||||
soapbody["use"] = "literal"
|
||||
|
||||
service = wsdl.add_child('wsdl:service')
|
||||
service["name"] = "%sService" % self.name
|
||||
service.add_child('wsdl:documentation', text=self.documentation)
|
||||
port=service.add_child('wsdl:port')
|
||||
port["name"] = "%s" % self.name
|
||||
port["binding"] = "tns:%sBinding" % self.name
|
||||
soapaddress = port.add_child('soap:address')
|
||||
soapaddress["location"] = self.location
|
||||
return wsdl.as_xml(pretty=True)
|
||||
|
||||
|
||||
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||
class SOAPHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
"User viewable help information and wsdl"
|
||||
args = self.path[1:].split("?")
|
||||
print "serving", args
|
||||
if self.path != "/" and args[0] not in self.server.dispatcher.methods.keys():
|
||||
self.send_error(404, "Method not found: %s" % args[0])
|
||||
else:
|
||||
if self.path == "/":
|
||||
# return wsdl if no method supplied
|
||||
response = self.server.dispatcher.wsdl()
|
||||
else:
|
||||
# return supplied method help (?request or ?response messages)
|
||||
req, res, doc = self.server.dispatcher.help(args[0])
|
||||
if len(args)==1 or args[1]=="request":
|
||||
response = req
|
||||
else:
|
||||
response = res
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/xml")
|
||||
self.end_headers()
|
||||
self.wfile.write(response)
|
||||
|
||||
def do_POST(self):
|
||||
"SOAP POST gateway"
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/xml")
|
||||
self.end_headers()
|
||||
request = self.rfile.read(int(self.headers.getheader('content-length')))
|
||||
response = self.server.dispatcher.dispatch(request)
|
||||
self.wfile.write(response)
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
import sys
|
||||
|
||||
dispatcher = SoapDispatcher(
|
||||
name = "PySimpleSoapSample",
|
||||
location = "http://localhost:8008/",
|
||||
action = 'http://localhost:8008/', # SOAPAction
|
||||
namespace = "http://example.com/pysimplesoapsamle/", prefix="ns0",
|
||||
documentation = 'Example soap service using PySimpleSoap',
|
||||
trace = True,
|
||||
ns = True)
|
||||
|
||||
def adder(p,c, dt=None):
|
||||
"Add several values"
|
||||
print c[0]['d'],c[1]['d'],
|
||||
import datetime
|
||||
dt = dt + datetime.timedelta(365)
|
||||
return {'ab': p['a']+p['b'], 'dd': c[0]['d']+c[1]['d'], 'dt': dt}
|
||||
|
||||
def dummy(in0):
|
||||
"Just return input"
|
||||
return in0
|
||||
|
||||
def echo(request):
|
||||
"Copy request->response (generic, any type)"
|
||||
return request.value
|
||||
|
||||
dispatcher.register_function('Adder', adder,
|
||||
returns={'AddResult': {'ab': int, 'dd': str } },
|
||||
args={'p': {'a': int,'b': int}, 'dt': Date, 'c': [{'d': Decimal}]})
|
||||
|
||||
dispatcher.register_function('Dummy', dummy,
|
||||
returns={'out0': str},
|
||||
args={'in0': str})
|
||||
|
||||
dispatcher.register_function('Echo', echo)
|
||||
|
||||
if '--local' in sys.argv:
|
||||
|
||||
wsdl=dispatcher.wsdl()
|
||||
print wsdl
|
||||
testfile = open("C:/test.wsdl","w")
|
||||
try:
|
||||
testfile.write(wsdl)
|
||||
finally:
|
||||
testfile.close()
|
||||
# dummy local test (clasic soap dialect)
|
||||
xml = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<soap:Body>
|
||||
<Adder xmlns="http://example.com/sample.wsdl">
|
||||
<p><a>1</a><b>2</b></p><c><d>5000000.1</d><d>.2</d></c><dt>20100724</dt>
|
||||
</Adder>
|
||||
</soap:Body>
|
||||
</soap:Envelope>"""
|
||||
|
||||
print dispatcher.dispatch(xml)
|
||||
|
||||
# dummy local test (modern soap dialect, SoapUI)
|
||||
xml = """
|
||||
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pys="http://example.com/pysimplesoapsamle/">
|
||||
<soapenv:Header/>
|
||||
<soapenv:Body>
|
||||
<pys:Adder>
|
||||
<pys:p><pys:a>9</pys:a><pys:b>3</pys:b></pys:p>
|
||||
<pys:dt>19690720<!--1969-07-20T21:28:00--></pys:dt>
|
||||
<pys:c><pys:d>10.001</pys:d><pys:d>5.02</pys:d></pys:c>
|
||||
</pys:Adder>
|
||||
</soapenv:Body>
|
||||
</soapenv:Envelope>
|
||||
"""
|
||||
print dispatcher.dispatch(xml)
|
||||
|
||||
# echo local test (generic soap service)
|
||||
xml = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
<soap:Body>
|
||||
<Echo xmlns="http://example.com/sample.wsdl">
|
||||
<value xsi:type="xsd:string">Hello world</value>
|
||||
</Echo>
|
||||
</soap:Body>
|
||||
</soap:Envelope>"""
|
||||
|
||||
print dispatcher.dispatch(xml)
|
||||
|
||||
|
||||
for method, doc in dispatcher.list_methods():
|
||||
request, response, doc = dispatcher.help(method)
|
||||
##print request
|
||||
##print response
|
||||
|
||||
if '--serve' in sys.argv:
|
||||
print "Starting server..."
|
||||
httpd = HTTPServer(("", 8008), SOAPHandler)
|
||||
httpd.dispatcher = dispatcher
|
||||
httpd.serve_forever()
|
||||
|
||||
if '--consume' in sys.argv:
|
||||
from client import SoapClient
|
||||
client = SoapClient(
|
||||
location = "http://localhost:8008/",
|
||||
action = 'http://localhost:8008/', # SOAPAction
|
||||
namespace = "http://example.com/sample.wsdl",
|
||||
soap_ns='soap',
|
||||
trace = True,
|
||||
ns = False)
|
||||
response = client.Adder(p={'a':1,'b':2},dt='20100724',c=[{'d':'1.20'},{'d':'2.01'}])
|
||||
result = response.AddResult
|
||||
print int(result.ab)
|
||||
print str(result.dd)
|
||||
|
||||
|
||||
|
||||
Executable
+416
@@ -0,0 +1,416 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: latin-1 -*-
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by the
|
||||
# Free Software Foundation; either version 3, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
|
||||
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
# for more details.
|
||||
|
||||
"Simple XML manipulation"
|
||||
|
||||
__author__ = "Mariano Reingart (reingart@gmail.com)"
|
||||
__copyright__ = "Copyright (C) 2008/009 Mariano Reingart"
|
||||
__license__ = "LGPL 3.0"
|
||||
__version__ = "1.02c"
|
||||
|
||||
import xml.dom.minidom
|
||||
from decimal import Decimal
|
||||
import datetime
|
||||
import time
|
||||
|
||||
DEBUG = False
|
||||
|
||||
# Functions to serialize/unserialize special immutable types:
|
||||
datetime_u = lambda s: datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%S")
|
||||
datetime_m = lambda dt: dt.isoformat('T')
|
||||
date_u = lambda s: datetime.datetime.strptime(s[0:10], "%Y-%m-%d").date()
|
||||
date_m = lambda d: d.strftime("%Y-%m-%d")
|
||||
time_u = lambda s: datetime.datetime.strptime(s, "%H:%M:%S").time()
|
||||
time_m = lambda d: d.strftime("%H%M%S")
|
||||
bool_u = lambda s: {'0':False, 'false': False, '1': True, 'true': True}[s]
|
||||
|
||||
# aliases:
|
||||
class Alias():
|
||||
def __init__(self, py_type, xml_type):
|
||||
self.py_type, self.xml_type = py_type, xml_type
|
||||
def __call__(self, value):
|
||||
return self.py_type(value)
|
||||
def __repr__(self):
|
||||
return "<alias '%s' for '%s'>" % (self.xml_type, self.py_type)
|
||||
|
||||
byte = Alias(str,'byte')
|
||||
short = Alias(int,'short')
|
||||
double = Alias(float,'double')
|
||||
integer = Alias(long,'integer')
|
||||
DateTime = datetime.datetime
|
||||
Date = datetime.date
|
||||
Time = datetime.time
|
||||
|
||||
# Define convertion function (python type): xml schema type
|
||||
TYPE_MAP = {str:'string',unicode:'string',
|
||||
bool:'boolean', short:'short', byte:'byte',
|
||||
int:'int', long:'long', integer:'integer',
|
||||
float:'float', double:'double',
|
||||
Decimal:'decimal',
|
||||
datetime.datetime:'dateTime', datetime.date:'date',
|
||||
}
|
||||
TYPE_MARSHAL_FN = {datetime.datetime:datetime_m, datetime.date:date_m,}
|
||||
TYPE_UNMARSHAL_FN = {datetime.datetime:datetime_u, datetime.date:date_u,
|
||||
bool:bool_u,
|
||||
}
|
||||
|
||||
|
||||
class OrderedDict(dict):
|
||||
"Minimal ordered dictionary for xsd:sequences"
|
||||
def __init__(self):
|
||||
self.__keys = []
|
||||
self.array = False
|
||||
def __setitem__(self, key, value):
|
||||
if key not in self.__keys:
|
||||
self.__keys.append(key)
|
||||
dict.__setitem__(self, key, value)
|
||||
def insert(self, key, value, index=0):
|
||||
if key not in self.__keys:
|
||||
self.__keys.insert(index, key)
|
||||
dict.__setitem__(self, key, value)
|
||||
def __delitem__(self, key):
|
||||
if key in self.__keys:
|
||||
self.__keys.remove(key)
|
||||
dict.__delitem__(self, key)
|
||||
def __iter__(self):
|
||||
return iter(self.__keys)
|
||||
def keys(self):
|
||||
return self.__keys
|
||||
def items(self):
|
||||
return [(key, self[key]) for key in self.__keys]
|
||||
def update(self, other):
|
||||
for k,v in other.items():
|
||||
self[k] = v
|
||||
if isinstance(other, OrderedDict):
|
||||
self.array = other.array
|
||||
def __str__(self):
|
||||
return "*%s*" % dict.__str__(self)
|
||||
def __repr__(self):
|
||||
s= "*{%s}*" % ", ".join(['%s: %s' % (repr(k),repr(v)) for k,v in self.items()])
|
||||
if self.array and False:
|
||||
s = "[%s]" % s
|
||||
return s
|
||||
|
||||
|
||||
class SimpleXMLElement(object):
|
||||
"Simple XML manipulation (simil PHP)"
|
||||
|
||||
def __init__(self, text = None, elements = None, document = None, namespace = None, prefix=None):
|
||||
self.__ns = namespace
|
||||
self.__prefix = prefix
|
||||
if text:
|
||||
try:
|
||||
self.__document = xml.dom.minidom.parseString(text)
|
||||
except:
|
||||
if DEBUG: print text
|
||||
raise
|
||||
self.__elements = [self.__document.documentElement]
|
||||
else:
|
||||
self.__elements = elements
|
||||
self.__document = document
|
||||
|
||||
def add_child(self,name,text=None,ns=True):
|
||||
"Adding a child tag to a node"
|
||||
if not ns or not self.__ns:
|
||||
if DEBUG: print "adding %s" % (name)
|
||||
element = self.__document.createElement(name)
|
||||
else:
|
||||
if DEBUG: print "adding %s ns %s %s" % (name, self.__ns,ns)
|
||||
if self.__prefix:
|
||||
element = self.__document.createElementNS(self.__ns, "%s:%s" % (self.__prefix, name))
|
||||
else:
|
||||
element = self.__document.createElementNS(self.__ns, name)
|
||||
if text:
|
||||
if isinstance(text, unicode):
|
||||
element.appendChild(self.__document.createTextNode(text))
|
||||
else:
|
||||
element.appendChild(self.__document.createTextNode(str(text)))
|
||||
self._element.appendChild(element)
|
||||
return SimpleXMLElement(
|
||||
elements=[element],
|
||||
document=self.__document,
|
||||
namespace=self.__ns,
|
||||
prefix=self.__prefix)
|
||||
|
||||
def __setattr__(self, tag, text):
|
||||
"Add text child tag node (short form)"
|
||||
if tag.startswith("_"):
|
||||
object.__setattr__(self, tag, text)
|
||||
else:
|
||||
if DEBUG: print "__setattr__(%s,%s)" % (tag, text)
|
||||
self.add_child(tag,text)
|
||||
|
||||
def add_comment(self, data):
|
||||
"Add an xml comment to this child"
|
||||
comment = self.__document.createComment(data)
|
||||
self._element.appendChild(comment)
|
||||
|
||||
def as_xml(self,filename=None,pretty=False):
|
||||
"Return the XML representation of the document"
|
||||
if not pretty:
|
||||
return self.__document.toxml('UTF-8')
|
||||
else:
|
||||
return self.__document.toprettyxml(encoding='UTF-8')
|
||||
|
||||
def __repr__(self):
|
||||
"Return the XML representation of this tag"
|
||||
return self._element.toxml('UTF-8')
|
||||
|
||||
def get_name(self):
|
||||
"Return the tag name of this node"
|
||||
return self._element.tagName
|
||||
|
||||
def get_local_name(self):
|
||||
"Return the tag loca name (prefix:name) of this node"
|
||||
return self._element.localName
|
||||
|
||||
def get_prefix(self):
|
||||
"Return the namespace prefix of this node"
|
||||
return self._element.prefix
|
||||
|
||||
def get_namespace_uri(self, ns):
|
||||
"Return the namespace uri for a prefix"
|
||||
v = self.__document.documentElement.attributes['xmlns:%s' % ns]
|
||||
return v.value
|
||||
|
||||
def attributes(self):
|
||||
"Return a dict of attributes for this tag"
|
||||
#TODO: use slice syntax [:]?
|
||||
return self._element.attributes
|
||||
|
||||
def __getitem__(self, item):
|
||||
"Return xml tag attribute value or a slice of attributes (iter)"
|
||||
if DEBUG: print "__getitem__(%s)" % item
|
||||
if isinstance(item,basestring):
|
||||
if self._element.hasAttribute(item):
|
||||
return self._element.attributes[item].value
|
||||
elif isinstance(item, slice):
|
||||
# return a list with name:values
|
||||
return self._element.attributes.items()[item]
|
||||
else:
|
||||
# return element by index (position)
|
||||
element = self.__elements[item]
|
||||
return SimpleXMLElement(
|
||||
elements=[element],
|
||||
document=self.__document,
|
||||
namespace=self.__ns,
|
||||
prefix=self.__prefix)
|
||||
|
||||
def add_attribute(self, name, value):
|
||||
"Set an attribute value from a string"
|
||||
self._element.setAttribute(name, value)
|
||||
|
||||
def __setitem__(self, item, value):
|
||||
"Set an attribute value"
|
||||
if isinstance(item,basestring):
|
||||
self.add_attribute(item, value)
|
||||
elif isinstance(item, slice):
|
||||
# set multiple attributes at once
|
||||
for k, v in value.items():
|
||||
self.add_attribute(k, v)
|
||||
|
||||
def __call__(self, tag=None, ns=None, children=False, error=True):
|
||||
"Search (even in child nodes) and return a child tag by name"
|
||||
try:
|
||||
if tag is None:
|
||||
# if no name given, iterate over siblings (same level)
|
||||
return self.__iter__()
|
||||
if children:
|
||||
# future: filter children? by ns?
|
||||
return self.children()
|
||||
elements = None
|
||||
if isinstance(tag, int):
|
||||
# return tag by index
|
||||
elements=[self.__elements[tag]]
|
||||
if ns and not elements:
|
||||
for ns_uri in isinstance(ns, (tuple, list)) and ns or (ns, ):
|
||||
if DEBUG: print "searching %s by ns=%s" % (tag,ns_uri)
|
||||
elements = self._element.getElementsByTagNameNS(ns_uri, tag)
|
||||
if elements:
|
||||
break
|
||||
if self.__ns and not elements:
|
||||
if DEBUG: print "searching %s by ns=%s" % (tag, self.__ns)
|
||||
elements = self._element.getElementsByTagNameNS(self.__ns, tag)
|
||||
if not elements:
|
||||
if DEBUG: print "searching %s " % (tag)
|
||||
elements = self._element.getElementsByTagName(tag)
|
||||
if not elements:
|
||||
if DEBUG: print self._element.toxml()
|
||||
if error:
|
||||
raise AttributeError("No elements found")
|
||||
else:
|
||||
return
|
||||
return SimpleXMLElement(
|
||||
elements=elements,
|
||||
document=self.__document,
|
||||
namespace=self.__ns,
|
||||
prefix=self.__prefix)
|
||||
except AttributeError, e:
|
||||
raise AttributeError("Tag not found: %s (%s)" % (tag, str(e)))
|
||||
|
||||
def __getattr__(self, tag):
|
||||
"Shortcut for __call__"
|
||||
return self.__call__(tag)
|
||||
|
||||
def __iter__(self):
|
||||
"Iterate over xml tags at this level"
|
||||
try:
|
||||
for __element in self.__elements:
|
||||
yield SimpleXMLElement(
|
||||
elements=[__element],
|
||||
document=self.__document,
|
||||
namespace=self.__ns,
|
||||
prefix=self.__prefix)
|
||||
except:
|
||||
raise
|
||||
|
||||
def __dir__(self):
|
||||
"List xml children tags names"
|
||||
return [node.tagName for node
|
||||
in self._element.childNodes
|
||||
if node.nodeType != node.TEXT_NODE]
|
||||
|
||||
def children(self):
|
||||
"Return xml children tags element"
|
||||
elements=[__element for __element in self._element.childNodes
|
||||
if __element.nodeType == __element.ELEMENT_NODE]
|
||||
if not elements:
|
||||
return None
|
||||
#raise IndexError("Tag %s has no children" % self._element.tagName)
|
||||
return SimpleXMLElement(
|
||||
elements=elements,
|
||||
document=self.__document,
|
||||
namespace=self.__ns,
|
||||
prefix=self.__prefix)
|
||||
|
||||
def __len__(self):
|
||||
"Return elements count"
|
||||
return len(self.__elements)
|
||||
|
||||
def __contains__( self, item):
|
||||
"Search for a tag name in this element or child nodes"
|
||||
return self._element.getElementsByTagName(item)
|
||||
|
||||
def __unicode__(self):
|
||||
"Returns the unicode text nodes of the current element"
|
||||
if self._element.childNodes:
|
||||
rc = u""
|
||||
for node in self._element.childNodes:
|
||||
if node.nodeType == node.TEXT_NODE:
|
||||
rc = rc + node.data
|
||||
return rc
|
||||
return ''
|
||||
|
||||
def __str__(self):
|
||||
"Returns the str text nodes of the current element"
|
||||
return unicode(self).encode("utf8","ignore")
|
||||
|
||||
def __int__(self):
|
||||
"Returns the integer value of the current element"
|
||||
return int(self.__str__())
|
||||
|
||||
def __float__(self):
|
||||
"Returns the float value of the current element"
|
||||
try:
|
||||
return float(self.__str__())
|
||||
except:
|
||||
raise IndexError(self._element.toxml())
|
||||
|
||||
_element = property(lambda self: self.__elements[0])
|
||||
|
||||
def unmarshall(self, types):
|
||||
"Convert to python values the current serialized xml element"
|
||||
# types is a dict of {tag name: convertion function}
|
||||
# example: types={'p': {'a': int,'b': int}, 'c': [{'d':str}]}
|
||||
# expected xml: <p><a>1</a><b>2</b></p><c><d>hola</d><d>chau</d>
|
||||
# returnde value: {'p': {'a':1,'b':2}, `'c':[{'d':'hola'},{'d':'chau'}]}
|
||||
d = {}
|
||||
for node in self():
|
||||
name = str(node.get_local_name())
|
||||
try:
|
||||
fn = types[name]
|
||||
except (KeyError, ), e:
|
||||
raise TypeError("Tag: %s invalid" % (name,))
|
||||
if isinstance(fn,list):
|
||||
value = []
|
||||
children = node.children()
|
||||
for child in children and children() or []:
|
||||
value.append(child.unmarshall(fn[0]))
|
||||
elif isinstance(fn,dict):
|
||||
children = node.children()
|
||||
value = children and children.unmarshall(fn)
|
||||
else:
|
||||
if fn is None: # xsd:anyType not unmarshalled
|
||||
value = node
|
||||
elif str(node) or fn == str:
|
||||
try:
|
||||
# get special desserialization function (if any)
|
||||
fn = TYPE_UNMARSHAL_FN.get(fn,fn)
|
||||
value = fn(unicode(node))
|
||||
except (ValueError, TypeError), e:
|
||||
raise ValueError("Tag: %s: %s" % (name, unicode(e)))
|
||||
else:
|
||||
value = None
|
||||
d[name] = value
|
||||
return d
|
||||
|
||||
def marshall(self, name, value, add_child=True, add_comments=False, ns=False):
|
||||
"Analize python value and add the serialized XML element using tag name"
|
||||
if isinstance(value, dict): # serialize dict (<key>value</key>)
|
||||
child = add_child and self.add_child(name,ns=ns) or self
|
||||
for k,v in value.items():
|
||||
child.marshall(k, v, add_comments=add_comments, ns=ns)
|
||||
elif isinstance(value, tuple): # serialize tuple (<key>value</key>)
|
||||
child = add_child and self.add_child(name,ns=ns) or self
|
||||
for k,v in value:
|
||||
getattr(self,name).marshall(k, v, add_comments=add_comments, ns=ns)
|
||||
elif isinstance(value, list): # serialize lists
|
||||
child=self.add_child(name,ns=ns)
|
||||
if add_comments:
|
||||
child.add_comment("Repetitive array of:")
|
||||
for t in value:
|
||||
child.marshall(name,t, False, add_comments=add_comments, ns=ns)
|
||||
elif isinstance(value, basestring): # do not convert strings or unicodes
|
||||
self.add_child(name,value,ns=ns)
|
||||
elif value is None: # sent a empty tag?
|
||||
self.add_child(name,ns=ns)
|
||||
elif value in TYPE_MAP.keys():
|
||||
# add commented placeholders for simple tipes (for examples/help only)
|
||||
child = self.add_child(name,ns=ns)
|
||||
child.add_comment(TYPE_MAP[value])
|
||||
else: # the rest of object types are converted to string
|
||||
# get special serialization function (if any)
|
||||
fn = TYPE_MARSHAL_FN.get(type(value),str)
|
||||
self.add_child(name,fn(value),ns=ns)
|
||||
|
||||
def import_node(self, other):
|
||||
x = self.__document.importNode(other._element, True) # deep copy
|
||||
self._element.appendChild(x)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
span = SimpleXMLElement('<span><a href="python.org.ar">pyar</a><prueba><i>1</i><float>1.5</float></prueba></span>')
|
||||
assert str(span.a)==str(span('a'))==str(span.a(0))=="pyar"
|
||||
assert span.a['href']=="python.org.ar"
|
||||
assert int(span.prueba.i)==1 and float(span.prueba.float)==1.5
|
||||
span1 = SimpleXMLElement('<span><a href="google.com">google</a><a>yahoo</a><a>hotmail</a></span>')
|
||||
assert [str(a) for a in span1.a()] == ['google', 'yahoo', 'hotmail']
|
||||
span1.add_child('a','altavista')
|
||||
span1.b = "ex msn"
|
||||
d = {'href':'http://www.bing.com/', 'alt': 'Bing'}
|
||||
span1.b[:] = d
|
||||
assert sorted([(k,v) for k,v in span1.b[:]]) == sorted(d.items())
|
||||
print span1.as_xml()
|
||||
assert 'b' in span1
|
||||
span.import_node(span1)
|
||||
print span.as_xml()
|
||||
|
||||
Reference in new issue
Block a user