Merge pull request #355 from reingart/master

debugger and soap contrib update and fixes
This commit is contained in:
mdipierro
2014-01-19 21:41:25 -08:00
12 changed files with 1557 additions and 945 deletions
+6 -2
View File
@@ -54,9 +54,13 @@ def interact():
filename = web_debugger.filename
lineno = web_debugger.lineno
if filename:
# prevent IOError 2 on some circuntances (EAFP instead of os.access)
try:
lines = open(filename).readlines()
except:
lines = ""
lines = dict([(i + 1, l) for (i, l) in enumerate(
[l.strip("\n").strip("\r") for l
in open(filename).readlines()])])
[l.strip("\n").strip("\r") for l in lines])])
filename = os.path.basename(filename)
else:
lines = {}
@@ -0,0 +1,53 @@
# coding: utf8
# SOAP webservices (server and client) example and basic test
# (using pysimplesoap contrib included in web2py)
# for more info see: https://code.google.com/p/pysimplesoap/wiki/Web2Py
from gluon.tools import Service
service = Service(globals())
# define the procedures to be exposed:
@service.soap('AddStrings', returns={'AddResult': str}, args={'a': str, 'b': str})
@service.soap('AddIntegers', returns={'AddResult': int}, args={'a': int, 'b': int})
def add(a, b):
"Add two values"
return a+b
@service.soap('SubIntegers', returns={'SubResult': int}, args={'a': int, 'b': int})
def sub(a, b):
"Substract two values"
return a-b
@service.soap('Division', returns={'divisionResult': float}, args={'a': float, 'b': float})
def division(a, b):
"Divide two values "
return a / b
# expose the soap methods
def call():
return service()
# sample function to test the SOAP RPC
def test_soap_sub():
from gluon.contrib.pysimplesoap.client import SoapClient, SoapFault
# build the url to the WSDL (web service description)
# like "http://localhost:8000/webservices/sample/call/soap?WSDL"
url = URL(f="call/soap", vars={"WSDL": ""}, scheme=True)
# create a SOAP client
client = SoapClient(wsdl=url)
# call the SOAP remote method
try:
ret = client.SubIntegers(a=3, b=2)
result = ret['SubResult']
except SoapFault, sf:
result = sf
response.view = "soap_examples/generic.html"
return dict(xml_request=client.xml_request,
xml_response=client.xml_response,
result=result)
@@ -0,0 +1,8 @@
{{extend 'layout.html'}}
<h1>SOAP Examples</h1>
<h2>Result</h2>
{{=BEAUTIFY(result)}}
<h2>XML Request</h2>
{{=BEAUTIFY(xml_request)}}
<h2>XML Response</h2>
{{=BEAUTIFY(xml_response)}}
+14 -5
View File
@@ -1,7 +1,16 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"PySimpleSOAP"
import client
import server
import simplexml
import transport
"""PySimpleSOAP"""
__author__ = "Mariano Reingart"
__author_email__ = "reingart@gmail.com"
__copyright__ = "Copyright (C) 2013 Mariano Reingart"
__license__ = "LGPL 3.0"
__version__ = "1.11"
TIMEOUT = 60
from . import client, server, simplexml, transport
File diff suppressed because it is too large Load Diff
+489
View File
@@ -0,0 +1,489 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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 helpers"""
from __future__ import unicode_literals
import sys
if sys.version > '3':
basestring = unicode = str
import datetime
from decimal import Decimal
import os
import logging
import hashlib
import warnings
try:
import urllib2
from urlparse import urlsplit
except ImportError:
from urllib import request as urllib2
from urllib.parse import urlsplit
from . import __author__, __copyright__, __license__, __version__
log = logging.getLogger(__name__)
def fetch(url, http, cache=False, force_download=False, wsdl_basedir=''):
"""Download a document from a URL, save it locally if cache enabled"""
# check / append a valid schema if not given:
url_scheme, netloc, path, query, fragment = urlsplit(url)
if not url_scheme in ('http', 'https', 'file'):
for scheme in ('http', 'https', 'file'):
try:
if not url.startswith("/") and scheme in ('http', 'https'):
tmp_url = "%s://%s" % (scheme, os.path.join(wsdl_basedir, url))
else:
tmp_url = "%s:%s" % (scheme, os.path.join(wsdl_basedir, url))
log.debug('Scheme not found, trying %s' % scheme)
return fetch(tmp_url, http, cache, force_download, wsdl_basedir)
except Exception as e:
log.error(e)
raise RuntimeError('No scheme given for url: %s' % url)
# make md5 hash of the url for caching...
filename = '%s.xml' % hashlib.md5(url.encode('utf8')).hexdigest()
if isinstance(cache, basestring):
filename = os.path.join(cache, filename)
if cache and os.path.exists(filename) and not force_download:
log.info('Reading file %s' % filename)
f = open(filename, 'r')
xml = f.read()
f.close()
else:
if url_scheme == 'file':
log.info('Fetching url %s using urllib2' % url)
f = urllib2.urlopen(url)
xml = f.read()
else:
log.info('GET %s using %s' % (url, http._wrapper_version))
response, xml = http.request(url, 'GET', None, {})
if cache:
log.info('Writing file %s' % filename)
if not os.path.isdir(cache):
os.makedirs(cache)
f = open(filename, 'w')
f.write(xml)
f.close()
return xml
def sort_dict(od, d):
"""Sort parameters (same order as xsd:sequence)"""
if isinstance(od, dict):
ret = OrderedDict()
for k in od.keys():
v = d.get(k)
# don't append null tags!
if v is not None:
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[k] = v
if hasattr(od, 'namespace'):
ret.namespace = od.namespace
ret.qualified = od.qualified
return ret
else:
return d
def make_key(element_name, element_type, namespace):
"""Return a suitable key for elements"""
# only distinguish 'element' vs other types
if element_type in ('complexType', 'simpleType'):
eltype = 'complexType'
else:
eltype = element_type
if eltype not in ('element', 'complexType', 'simpleType'):
raise RuntimeError("Unknown element type %s = %s" % (element_name, eltype))
return (element_name, eltype, namespace)
def process_element(elements, element_name, node, element_type, xsd_uri, dialect, namespace, qualified=None,
soapenc_uri = 'http://schemas.xmlsoap.org/soap/encoding/'):
"""Parse and define simple element types"""
log.debug('Processing element %s %s' % (element_name, element_type))
for tag in node:
if tag.get_local_name() in ('annotation', 'documentation'):
continue
elif tag.get_local_name() in ('element', 'restriction'):
log.debug('%s has no children! %s' % (element_name, tag))
children = tag # element "alias"?
alias = True
elif tag.children():
children = tag.children()
alias = False
else:
log.debug('%s has no children! %s' % (element_name, tag))
continue # TODO: abstract?
d = OrderedDict()
d.namespace = namespace
d.qualified = qualified
for e in children:
t = e['type']
if not t:
t = e['base'] # complexContent (extension)!
if not t:
t = e['ref'] # reference to another element
if not t:
# "anonymous" elements had no type attribute but children
if e['name'] and e.children():
# create a type name to process the children
t = "%s_%s" % (element_name, e['name'])
c = e.children()
et = c.get_local_name()
c = c.children()
process_element(elements, t, c, et, xsd_uri, dialect, namespace, qualified)
else:
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 and not alias and len(children) > 1:
continue # abort to prevent infinite recursion
uri = ns and e.get_namespace_uri(ns) or xsd_uri
if uri in (xsd_uri, soapenc_uri) and type_name != 'Array':
# look for the type, None == any
fn = REVERSE_TYPE_MAP.get(type_name, None)
elif uri == soapenc_uri and type_name == 'Array':
# arrays of simple types (look at the attribute tags):
fn = []
for a in e.children():
for k, v in a[:]:
if k.endswith(":arrayType"):
type_name = v
if ":" in type_name:
type_name = type_name[type_name.index(":")+1:]
if "[]" in type_name:
type_name = type_name[:type_name.index("[]")]
fn.append(REVERSE_TYPE_MAP.get(type_name, None))
else:
fn = None
if not fn:
# simple / complex type, postprocess later
if ns:
fn_namespace = uri # use the specified namespace
else:
fn_namespace = namespace # use parent namespace (default)
for k, v in e[:]:
if k.startswith("xmlns:"):
# get the namespace uri from the element
fn_namespace = v
fn = elements.setdefault(make_key(type_name, 'complexType', fn_namespace), OrderedDict())
if e['maxOccurs'] == 'unbounded' or (uri == soapenc_uri and type_name == 'Array'):
# it's an array... TODO: compound arrays? and check ns uri!
if isinstance(fn, OrderedDict):
if len(children) > 1 and dialect in ('jetty',):
# Jetty style support
# {'ClassName': [{'attr1': val1, 'attr2': val2}]
fn.array = True
else:
# .NET style support (backward compatibility)
# [{'ClassName': {'attr1': val1, 'attr2': val2}]
d.array = True
else:
if dialect in ('jetty',):
# scalar support [{'attr1': [val1]}]
fn = [fn]
else:
d.array = True
if (e['name'] is not None and not alias) or e['ref']:
e_name = e['name'] or type_name # for refs, use the type name
d[e_name] = fn
else:
log.debug('complexContent/simpleType/element %s = %s' % (element_name, type_name))
d[None] = fn
if e is not None and e.get_local_name() == 'extension' and e.children():
# extend base element:
process_element(elements, element_name, e.children(), element_type, xsd_uri, dialect, namespace, qualified)
elements.setdefault(make_key(element_name, element_type, namespace), OrderedDict()).update(d)
def postprocess_element(elements, processed):
"""Fix unresolved references (elements referenced before its definition, thanks .net)"""
# avoid already processed elements:
if elements in processed:
return
processed.append(elements)
for k, v in elements.items():
if isinstance(v, OrderedDict):
if v != elements: # TODO: fix recursive elements
postprocess_element(v, processed)
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-
if v[None] is not None:
elements[k].insert(kk, v[None][kk], i)
del v[None]
else: # "alias", just replace
log.debug('Replacing %s = %s' % (k, v[None]))
elements[k] = v[None]
#break
if v.array:
elements[k] = [v] # convert arrays to python lists
if isinstance(v, list):
for n in v: # recurse list
if isinstance(n, (OrderedDict, list)):
#if n != elements: # TODO: fix recursive elements
postprocess_element(n, processed)
def get_message(messages, message_name, part_name):
if part_name:
# get the specific part of the message:
return messages.get((message_name, part_name))
else:
# get the first part for the specified message:
for (message_name_key, part_name_key), message in messages.items():
if message_name_key == message_name:
return message
get_local_name = lambda s: s and str((':' in s) and s.split(':')[1] or s)
get_namespace_prefix = lambda s: s and str((':' in s) and s.split(':')[0] or None)
def preprocess_schema(schema, imported_schemas, elements, xsd_uri, dialect, http, cache, force_download, wsdl_basedir, global_namespaces=None, qualified=False):
"""Find schema elements and complex types"""
from .simplexml import SimpleXMLElement # here to avoid recursive imports
# analyze the namespaces used in this schema
local_namespaces = {}
for k, v in schema[:]:
if k.startswith("xmlns"):
local_namespaces[get_local_name(k)] = v
if k == 'targetNamespace':
# URI namespace reference for this schema
if v == "urn:DefaultNamespace":
v = global_namespaces[None]
local_namespaces[None] = v
if k == 'elementFormDefault':
qualified = (v == "qualified")
# add schema namespaces to the global namespace dict = {URI: ns prefix}
for ns in local_namespaces.values():
if ns not in global_namespaces:
global_namespaces[ns] = 'ns%s' % len(global_namespaces)
for element in schema.children() or []:
if element.get_local_name() in ('import', 'include',):
schema_namespace = element['namespace']
schema_location = element['schemaLocation']
if schema_location is None:
log.debug('Schema location not provided for %s!' % schema_namespace)
continue
if schema_location in imported_schemas:
log.debug('Schema %s already imported!' % schema_location)
continue
imported_schemas[schema_location] = schema_namespace
log.debug('Importing schema %s from %s' % (schema_namespace, schema_location))
# Open uri and read xml:
xml = fetch(schema_location, http, cache, force_download, wsdl_basedir)
# Parse imported XML schema (recursively):
imported_schema = SimpleXMLElement(xml, namespace=xsd_uri)
preprocess_schema(imported_schema, imported_schemas, elements, xsd_uri, dialect, http, cache, force_download, wsdl_basedir, global_namespaces, qualified)
element_type = element.get_local_name()
if element_type in ('element', 'complexType', "simpleType"):
namespace = local_namespaces[None] # get targetNamespace
element_ns = global_namespaces[ns] # get the prefix
element_name = element['name']
log.debug("Parsing Element %s: %s" % (element_type, 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(elements, element_name, children, element_type, xsd_uri, dialect, namespace, qualified)
# simplexml utilities:
try:
_strptime = datetime.datetime.strptime
except AttributeError: # python2.4
_strptime = lambda s, fmt: datetime.datetime(*(time.strptime(s, fmt)[:6]))
# Functions to serialize/deserialize special immutable types:
def datetime_u(s):
fmt = "%Y-%m-%dT%H:%M:%S"
try:
return _strptime(s, fmt)
except ValueError:
try:
# strip utc offset
if s[-3] == ":" and s[-6] in (' ', '-', '+'):
warnings.warn('removing unsupported UTC offset', RuntimeWarning)
s = s[:-6]
# parse microseconds
try:
return _strptime(s, fmt + ".%f")
except:
return _strptime(s, fmt)
except ValueError:
# strip microseconds (not supported in this platform)
if "." in s:
warnings.warn('removing unsuppported microseconds', RuntimeWarning)
s = s[:s.index(".")]
return _strptime(s, fmt)
datetime_m = lambda dt: dt.isoformat()
date_u = lambda s: _strptime(s[0:10], "%Y-%m-%d").date()
date_m = lambda d: d.strftime("%Y-%m-%d")
time_u = lambda s: _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]
bool_m = lambda s: {False: 'false', True: 'true'}[s]
# aliases:
class Alias(object):
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)
if sys.version > '3':
long = Alias(int, 'long')
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 = {
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,
bool: bool_m
}
TYPE_UNMARSHAL_FN = {
datetime.datetime: datetime_u,
datetime.date: date_u,
bool: bool_u,
str: unicode,
}
REVERSE_TYPE_MAP = dict([(v, k) for k, v in TYPE_MAP.items()])
REVERSE_TYPE_MAP.update({
'base64Binary': str,
})
# insert str here to avoid collision in REVERSE_TYPE_MAP (i.e. decoding errors)
if str not in TYPE_MAP:
TYPE_MAP[str] = 'string'
class OrderedDict(dict):
"""Minimal ordered dictionary for xsd:sequences"""
def __init__(self):
self.__keys = []
self.array = False
self.namespace = None
self.qualified = None
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
# do not change if we are an array but the other is not:
if isinstance(other, OrderedDict) and not self.array:
self.array = other.array
if isinstance(other, OrderedDict) and not self.namespace:
self.namespace = other.namespace
self.qualified = other.qualified
def copy(self):
"Make a duplicate"
new = OrderedDict()
new.update(self)
return new
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
+236 -146
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: latin-1 -*-
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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
@@ -10,30 +10,41 @@
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
"Simple SOAP Server implementation"
"""Pythonic simple SOAP Server implementation"""
__author__ = "Mariano Reingart (reingart@gmail.com)"
__copyright__ = "Copyright (C) 2010 Mariano Reingart"
__license__ = "LGPL 3.0"
__version__ = "1.03c"
from __future__ import unicode_literals
import sys
if sys.version > '3':
unicode = str
import datetime
import sys
import logging
import warnings
import re
import traceback
from simplexml import SimpleXMLElement, TYPE_MAP, Date, Decimal
try:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from http.server import BaseHTTPRequestHandler, HTTPServer
from . import __author__, __copyright__, __license__, __version__
from .simplexml import SimpleXMLElement, TYPE_MAP, Date, Decimal
log = logging.getLogger(__name__)
# Deprecated
DEBUG = False
NS_RX=re.compile(r'xmlns:(\w+)="(.+?)"')
# Deprecated?
NS_RX = re.compile(r'xmlns:(\w+)="(.+?)"')
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/",
"""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',
namespaces={},
pretty=False,
@@ -45,13 +56,13 @@ class SoapDispatcher(object):
:param namespaces: Specify additional namespaces; example: {'external': 'http://external.mt.moboperator'}
:param pretty: Prettifies generated xmls
:param debug: Use to add tracebacks in generated xmls.
Multiple namespaces
===================
It is possible to support multiple namespaces.
You need to specify additional namespaces by passing `namespace` parameter.
>>> dispatcher = SoapDispatcher(
... name = "MTClientWS",
... location = "http://localhost:8008/ws/MTClientWS",
@@ -59,13 +70,13 @@ class SoapDispatcher(object):
... namespace = "http://external.mt.moboperator", prefix="external",
... documentation = 'moboperator MTClientWS',
... namespaces = {
... 'external': 'http://external.mt.moboperator',
... 'external': 'http://external.mt.moboperator',
... 'model': 'http://model.common.mt.moboperator'
... },
... ns = True)
Now the registered method must return node names with namespaces' prefixes.
>>> def _multi_ns_func(self, serviceMsisdn):
... ret = {
... 'external:activateSubscriptionsReturn': [
@@ -73,23 +84,22 @@ class SoapDispatcher(object):
... {'model:description': 'desc'},
... ]}
... return ret
Our prefixes will be changed to those used by the client.
"""
self.methods = {}
self.name = name
self.documentation = documentation
self.action = action # base SoapAction
self.action = action # base SoapAction
self.location = location
self.namespace = namespace # targetNamespace
self.namespace = namespace # targetNamespace
self.prefix = prefix
self.soap_ns = soap_ns
self.soap_uri = soap_uri
self.namespaces = namespaces
self.pretty = pretty
self.debug = debug
@staticmethod
def _extra_namespaces(xml, ns):
"""Extends xml with extra namespaces.
@@ -99,56 +109,57 @@ class SoapDispatcher(object):
if ns:
_tpl = 'xmlns:%s="%s"'
_ns_str = " ".join([_tpl % (prefix, uri) for uri, prefix in ns.items() if uri not in xml])
xml = xml.replace('/>', ' '+_ns_str+'/>')
xml = xml.replace('/>', ' ' + _ns_str + '/>')
return xml
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"
def dispatch(self, xml, action=None, fault=None):
"""Receive and process SOAP call, returns the xml"""
# a dict can be sent in fault to expose it to the caller
# default values:
prefix = self.prefix
ret = fault = None
ret = None
if fault is None:
fault = {}
soap_ns, soap_uri = self.soap_ns, self.soap_uri
soap_fault_code = 'VersionMismatch'
name = None
# namespaces = [('model', 'http://model.common.mt.moboperator'), ('external', 'http://external.mt.moboperator')]
_ns_reversed = dict(((v,k) for k,v in self.namespaces.iteritems())) # Switch keys-values
_ns_reversed = dict(((v, k) for k, v in self.namespaces.items())) # Switch keys-values
# _ns_reversed = {'http://external.mt.moboperator': 'external', 'http://model.common.mt.moboperator': 'model'}
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",):
"http://www.w3.org/2003/05/soap-env",):
soap_ns = request.attributes()[k].localName
soap_uri = request.attributes()[k].value
# If the value from attributes on Envelope is in additional namespaces
elif v in self.namespaces.values():
_ns = request.attributes()[k].localName
_uri = request.attributes()[k].value
_ns_reversed[_uri] = _ns # update with received alias
_ns_reversed[_uri] = _ns # update with received alias
# Now we change 'external' and 'model' to the received forms i.e. 'ext' and 'mod'
# After that we know how the client has prefixed additional namespaces
ns = NS_RX.findall(xml)
for k, v in ns:
if v in self.namespaces.values():
_ns_reversed[v] = k
soap_fault_code = 'Client'
# parse request message and get local method
method = request('Body', ns=soap_uri).children()(0)
if action:
# method name = action
# method name = action
name = action[len(self.action)+1:-1]
prefix = self.prefix
if not action or not name:
@@ -159,21 +170,21 @@ class SoapDispatcher(object):
log.debug('dispatch method: %s', name)
function, returns_types, args_types, doc = self.methods[name]
log.debug('returns_types %s', returns_types)
# 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
args = {'request': method} # send raw request
else:
args = {} # no parameters
args = {} # no parameters
soap_fault_code = 'Server'
# execute function
ret = function(**args)
log.debug('dispathed method returns: %s', ret)
except Exception: # This shouldn't be one huge try/except
except Exception: # This shouldn't be one huge try/except
import sys
etype, evalue, etb = sys.exc_info()
log.error(traceback.format_exc())
@@ -182,43 +193,43 @@ class SoapDispatcher(object):
detail += '\n\nXML REQUEST\n\n' + xml
else:
detail = None
fault = {'faultcode': "%s.%s" % (soap_fault_code, etype.__name__),
'faultstring': unicode(evalue),
'detail': detail}
fault.update({'faultcode': "%s.%s" % (soap_fault_code, etype.__name__),
'faultstring': evalue,
'detail': detail})
# build response message
if not prefix:
xml = """<%(soap_ns)s:Envelope xmlns:%(soap_ns)s="%(soap_uri)s"/>"""
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"/>"""
xmlns:%(prefix)s="%(namespace)s"/>"""
xml %= { # a %= {} is a shortcut for a = a % {}
'namespace': self.namespace,
'namespace': self.namespace,
'prefix': prefix,
'soap_ns': soap_ns,
'soap_ns': soap_ns,
'soap_uri': soap_uri
}
# Now we add extra namespaces
xml = SoapDispatcher._extra_namespaces(xml, _ns_reversed)
# Change our namespace alias to that given by the client.
# We put [('model', 'http://model.common.mt.moboperator'), ('external', 'http://external.mt.moboperator')]
# mix it with {'http://external.mt.moboperator': 'ext', 'http://model.common.mt.moboperator': 'mod'}
mapping = dict(((k, _ns_reversed[v]) for k,v in self.namespaces.iteritems())) # Switch keys-values and change value
mapping = dict(((k, _ns_reversed[v]) for k, v in self.namespaces.items())) # Switch keys-values and change value
# and get {'model': u'mod', 'external': u'ext'}
response = SimpleXMLElement(xml,
response = SimpleXMLElement(xml,
namespace=self.namespace,
namespaces_map = mapping,
namespaces_map=mapping,
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)
@@ -226,14 +237,24 @@ class SoapDispatcher(object):
# return normal value
res = body.add_child("%sResponse" % name, ns=prefix)
if not prefix:
res['xmlns'] = self.namespace # add target namespace
res['xmlns'] = self.namespace # add target namespace
# serialize returned values (response) if type definition available
if returns_types:
if not isinstance(ret, dict):
# TODO: full sanity check of type structure (recursive)
complex_type = isinstance(ret, dict)
if complex_type:
# check if type mapping correlates with return value
types_ok = all([k in returns_types for k in ret.keys()])
if not types_ok:
warnings.warn("Return value doesn't match type structure: "
"%s vs %s" % (str(returns_types), str(ret)))
if not complex_type or not types_ok:
# backward compatibility for scalar and simple types
res.marshall(returns_types.keys()[0], ret, )
else:
for k,v in ret.items():
# new style for complex classes
for k, v in ret.items():
res.marshall(k, v)
elif returns_types is None:
# merge xmlelement returned
@@ -246,16 +267,16 @@ class SoapDispatcher(object):
# 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()]
"""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"
"""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}
</soap:Envelope>""" % {'method': method, 'namespace': self.namespace}
request = SimpleXMLElement(xml, namespace=self.namespace, prefix=self.prefix)
if args:
items = args.items()
@@ -263,13 +284,13 @@ class SoapDispatcher(object):
items = [('value', None)]
else:
items = []
for k,v in 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}
</soap:Envelope>""" % {'method': method, 'namespace': self.namespace}
response = SimpleXMLElement(xml, namespace=self.namespace, prefix=self.prefix)
if returns:
items = returns.items()
@@ -277,16 +298,15 @@ class SoapDispatcher(object):
items = [('value', None)]
else:
items = []
for k,v in items:
response('%sResponse'%method).marshall(k, v, add_comments=True, ns=False)
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"
"""Generate Web Service Description v1.1"""
xml = """<?xml version="1.0"?>
<wsdl:definitions name="%(name)s"
<wsdl:definitions name="%(name)s"
targetNamespace="%(namespace)s"
xmlns:tns="%(namespace)s"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
@@ -307,7 +327,7 @@ class SoapDispatcher(object):
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')
@@ -326,38 +346,40 @@ class SoapDispatcher(object):
all = complex.add_child("xsd:all")
elif items:
all = complex.add_child("xsd:sequence")
for k,v in items:
for k, v in items:
e = all.add_child("xsd:element")
e['name'] = k
if array:
e[:]={'minOccurs': "0", 'maxOccurs': "unbounded"}
e[:] = {'minOccurs': "0", 'maxOccurs': "unbounded"}
if v in TYPE_MAP.keys():
t='xsd:%s' % TYPE_MAP[v]
t = 'xsd:%s' % TYPE_MAP[v]
elif v is None:
t='xsd:anyType'
t = 'xsd:anyType'
elif isinstance(v, list):
n="ArrayOf%s%s" % (name, k)
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)
elif isinstance(v, dict):
n = "%s%s" % (name, k)
parse_element(n, v.items(), complex=True)
t = "tns:%s" % n
else:
raise TypeError("unknonw type v for marshalling" % str(v))
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'):
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)}
part[:] = {'name': 'parameters',
'element': 'tns:%s%s' % (method, e)}
# create ports
portType = wsdl.add_child('wsdl:portType')
@@ -397,18 +419,18 @@ class SoapDispatcher(object):
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 = 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"
"""User viewable help information and wsdl"""
args = self.path[1:].split("?")
if self.path != "/" and args[0] not in self.server.dispatcher.methods.keys():
self.send_error(404, "Method not found: %s" % args[0])
@@ -419,92 +441,160 @@ class SOAPHandler(BaseHTTPRequestHandler):
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":
if len(args) == 1 or args[1] == "request":
response = req
else:
response = res
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)
"""SOAP POST gateway"""
request = self.rfile.read(int(self.headers.getheader('content-length')))
fault = {}
# execute the method
response = self.server.dispatcher.dispatch(request, fault=fault)
# check if fault dict was completed (faultcode, faultstring, detail)
if fault:
self.send_response(500)
else:
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
class WSGISOAPHandler(object):
def __init__(self, dispatcher):
self.dispatcher = dispatcher
def __call__(self, environ, start_response):
return self.handler(environ, start_response)
def handler(self, environ, start_response):
if environ['REQUEST_METHOD'] == 'GET':
return self.do_get(environ, start_response)
elif environ['REQUEST_METHOD'] == 'POST':
return self.do_post(environ, start_response)
else:
start_response('405 Method not allowed', [('Content-Type', 'text/plain')])
return ['Method not allowed']
def do_get(self, environ, start_response):
path = environ.get('PATH_INFO').lstrip('/')
query = environ.get('QUERY_STRING')
if path != "" and path not in self.dispatcher.methods.keys():
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return ["Method not found: %s" % path]
elif path == "":
# return wsdl if no method supplied
response = self.dispatcher.wsdl()
else:
# return supplied method help (?request or ?response messages)
req, res, doc = self.dispatcher.help(path)
if len(query) == 0 or query == "request":
response = req
else:
response = res
start_response('200 OK', [('Content-Type', 'text/xml'), ('Content-Length', str(len(response)))])
return [response]
def do_post(self, environ, start_response):
length = int(environ['CONTENT_LENGTH'])
request = environ['wsgi.input'].read(length)
response = self.dispatcher.dispatch(request)
start_response('200 OK', [('Content-Type', 'text/xml'), ('Content-Length', str(len(response)))])
return [response]
if __name__ == "__main__":
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
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, debug=True,
ns=True)
def adder(p, c, dt=None):
"""Add several values"""
dt = dt + datetime.timedelta(365)
return {'ab': p['a']+p['b'], 'dd': c[0]['d']+c[1]['d'], 'dt': dt}
return {'ab': p['a'] + p['b'], 'dd': c[0]['d'] + c[1]['d'], 'dt': dt}
def dummy(in0):
"Just return input"
"""Just return input"""
return in0
def echo(request):
"Copy request->response (generic, any type)"
"""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(
'Adder', adder,
returns={'AddResult': {'ab': int, 'dd': unicode, 'dt': datetime.date}},
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(
'Dummy', dummy,
returns={'out0': str},
args={'in0': str}
)
dispatcher.register_function('Echo', echo)
if '--local' in sys.argv:
wsdl=dispatcher.wsdl()
print wsdl
# Commented because path is platform dependent
# Looks that it doesnt matter.
# open("C:/test.wsdl","w").write(wsdl)
wsdl = dispatcher.wsdl()
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..."
log.info("Starting server...")
httpd = HTTPServer(("", 8008), SOAPHandler)
httpd.dispatcher = dispatcher
httpd.serve_forever()
if '--wsgi-serve' in sys.argv:
log.info("Starting wsgi server...")
from wsgiref.simple_server import make_server
application = WSGISOAPHandler(dispatcher)
wsgid = make_server('', 8008, application)
wsgid.serve_forever()
if '--consume' in sys.argv:
from client import SoapClient
from .client import SoapClient
client = SoapClient(
location = "http://localhost:8008/",
action = 'http://localhost:8008/', # SOAPAction
namespace = "http://example.com/sample.wsdl",
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'}])
trace=True,
ns=False
)
p = {'a': 1, 'b': 2}
c = [{'d': '1.20'}, {'d': '2.01'}]
response = client.Adder(p=p, dt='2010-07-24', c=c)
result = response.AddResult
print int(result.ab)
print str(result.dd)
log.info(int(result.ab))
log.info(str(result.dd))
if '--consume-wsdl' in sys.argv:
from .client import SoapClient
client = SoapClient(
wsdl="http://localhost:8008/",
)
p = {'a': 1, 'b': 2}
c = [{'d': '1.20'}, {'d': '2.01'}]
dt = datetime.date.today()
response = client.Adder(p=p, dt=dt, c=c)
result = response['AddResult']
log.info(int(result['ab']))
log.info(str(result['dd']))
+185 -262
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: latin-1 -*-
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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
@@ -10,164 +10,47 @@
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
"Simple XML manipulation"
"""Simple XML manipulation"""
__author__ = "Mariano Reingart (reingart@gmail.com)"
__copyright__ = "Copyright (C) 2008/009 Mariano Reingart"
__license__ = "LGPL 3.0"
__version__ = "1.03a"
import datetime
from __future__ import unicode_literals
import sys
if sys.version > '3':
basestring = str
unicode = str
import logging
import re
import time
import warnings
import xml.dom.minidom
from decimal import Decimal
from . import __author__, __copyright__, __license__, __version__
# Utility functions used for marshalling, moved aside for readability
from .helpers import TYPE_MAP, TYPE_MARSHAL_FN, TYPE_UNMARSHAL_FN, \
REVERSE_TYPE_MAP, OrderedDict, Date, Decimal
log = logging.getLogger(__name__)
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARNING)
DEBUG = False
try:
_strptime = datetime.datetime.strptime
except AttributeError: # python2.4
_strptime = lambda s, fmt: datetime.datetime(*(time.strptime(s, fmt)[:6]))
# Functions to serialize/deserialize special immutable types:
def datetime_u(s):
fmt = "%Y-%m-%dT%H:%M:%S"
try:
return _strptime(s, fmt)
except ValueError:
try:
# strip utc offset
if s[-3] == ":" and s[-6] in (' ', '-', '+'):
warnings.warn('removing unsupported UTC offset', RuntimeWarning)
s = s[:-6]
# parse microseconds
try:
return _strptime(s, fmt + ".%f")
except:
return _strptime(s, fmt)
except ValueError:
# strip microseconds (not supported in this platform)
if "." in s:
warnings.warn('removing unsuppported microseconds', RuntimeWarning)
s = s[:s.index(".")]
return _strptime(s, fmt)
datetime_m = lambda dt: dt.isoformat('T')
date_u = lambda s: _strptime(s[0:10], "%Y-%m-%d").date()
date_m = lambda d: d.strftime("%Y-%m-%d")
time_u = lambda s: _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]
bool_m = lambda s: {False: 'false', True: 'true'}[s]
# aliases:
class Alias(object):
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,
bool:bool_m
}
TYPE_UNMARSHAL_FN = {
datetime.datetime:datetime_u,
datetime.date:date_u,
bool:bool_u,
str:unicode,
}
REVERSE_TYPE_MAP = dict([(v,k) for k,v in TYPE_MAP.items()])
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, namespaces_map={}):
"""Simple XML manipulation (simil PHP)"""
def __init__(self, text=None, elements=None, document=None,
namespace=None, prefix=None, namespaces_map={}, jetty=False):
"""
:param namespaces_map: How to map our namespace prefix to that given by the client;
{prefix: received_prefix}
"""
self.__namespaces_map = namespaces_map
_rx = "|".join(namespaces_map.keys()) # {'external': 'ext', 'model': 'mod'} -> 'external|model'
self.__ns_rx = re.compile(r"^(%s):.*$" % _rx) # And now we build an expression ^(external|model):.*$
# to find prefixes in all xml nodes i.e.: <model:code>1</model:code>
# and later change that to <mod:code>1</mod:code>
_rx = "|".join(namespaces_map.keys()) # {'external': 'ext', 'model': 'mod'} -> 'external|model'
self.__ns_rx = re.compile(r"^(%s):.*$" % _rx) # And now we build an expression ^(external|model):.*$
# to find prefixes in all xml nodes i.e.: <model:code>1</model:code>
# and later change that to <mod:code>1</mod:code>
self.__ns = namespace
self.__prefix = prefix
self.__jetty = jetty # special list support
if text is not None:
try:
self.__document = xml.dom.minidom.parseString(text)
@@ -178,78 +61,81 @@ class SimpleXMLElement(object):
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:
log.debug('adding %s', name)
"""Adding a child tag to a node"""
if not ns or self.__ns is False:
##log.debug('adding %s without namespace', name)
element = self.__document.createElement(name)
else:
log.debug('adding %s ns "%s" %s', name, self.__ns, ns)
if self.__prefix:
##log.debug('adding %s ns "%s" %s', name, self.__ns, ns)
if isinstance(ns, basestring):
element = self.__document.createElement(name)
if ns:
element.setAttribute("xmlns", ns)
elif self.__prefix:
element = self.__document.createElementNS(self.__ns, "%s:%s" % (self.__prefix, name))
else:
element = self.__document.createElementNS(self.__ns, name)
# don't append null tags!
if text is not None:
if isinstance(text, unicode):
element.appendChild(self.__document.createTextNode(text))
else:
element.appendChild(self.__document.createTextNode(str(text)))
element.appendChild(self.__document.createTextNode(text))
self._element.appendChild(element)
return SimpleXMLElement(
elements=[element],
document=self.__document,
namespace=self.__ns,
prefix=self.__prefix,
namespaces_map=self.__namespaces_map)
elements=[element],
document=self.__document,
namespace=self.__ns,
prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map
)
def __setattr__(self, tag, text):
"Add text child tag node (short form)"
"""Add text child tag node (short form)"""
if tag.startswith("_"):
object.__setattr__(self, tag, text)
else:
log.debug('__setattr__(%s, %s)', tag, text)
##log.debug('__setattr__(%s, %s)', tag, text)
self.add_child(tag, text)
def __delattr__(self, tag):
"Remove a child tag (non recursive!)"
elements=[__element for __element in self._element.childNodes
if __element.nodeType == __element.ELEMENT_NODE
]
"""Remove a child tag (non recursive!)"""
elements = [__element for __element in self._element.childNodes
if __element.nodeType == __element.ELEMENT_NODE]
for element in elements:
self._element.removeChild(element)
def add_comment(self, data):
"Add an xml comment to this child"
"""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"
"""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 the XML representation of this tag"""
# NOTE: do not use self.as_xml('UTF-8') as it returns the whole xml doc
return self._element.toxml('UTF-8')
def get_name(self):
"Return the tag name of this node"
"""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 the tag local name (prefix:name) of this node"""
return self._element.localName
def get_prefix(self):
"Return the namespace prefix of this node"
"""Return the namespace prefix of this node"""
return self._element.prefix
def get_namespace_uri(self, ns):
"Return the namespace uri for a prefix"
"""Return the namespace uri for a prefix"""
element = self._element
while element is not None and element.attributes is not None:
try:
@@ -257,38 +143,39 @@ class SimpleXMLElement(object):
except KeyError:
element = element.parentNode
def attributes(self):
"Return a dict of attributes for this tag"
"""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)"
log.debug('__getitem__(%s)', item)
"""Return xml tag attribute value or a slice of attributes (iter)"""
##log.debug('__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]
return list(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,
namespaces_map=self.__namespaces_map)
elements=[element],
document=self.__document,
namespace=self.__ns,
prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map
)
def add_attribute(self, name, value):
"Set an attribute value from a string"
"""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):
"""Set an attribute value"""
if isinstance(item, basestring):
self.add_attribute(item, value)
elif isinstance(item, slice):
# set multiple attributes at once
@@ -297,7 +184,7 @@ class SimpleXMLElement(object):
def __call__(self, tag=None, ns=None, children=False, root=False,
error=True, ):
"Search (even in child nodes) and return a child tag by name"
"""Search (even in child nodes) and return a child tag by name"""
try:
if root:
# return entire document
@@ -306,6 +193,7 @@ class SimpleXMLElement(object):
document=self.__document,
namespace=self.__ns,
prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map
)
if tag is None:
@@ -317,23 +205,23 @@ class SimpleXMLElement(object):
elements = None
if isinstance(tag, int):
# return tag by index
elements=[self.__elements[tag]]
elements = [self.__elements[tag]]
if ns and not elements:
for ns_uri in isinstance(ns, (tuple, list)) and ns or (ns, ):
log.debug('searching %s by ns=%s', tag, ns_uri)
##log.debug('searching %s by ns=%s', tag, ns_uri)
elements = self._element.getElementsByTagNameNS(ns_uri, tag)
if elements:
if elements:
break
if self.__ns and not elements:
log.debug('searching %s by ns=%s', tag, self.__ns)
##log.debug('searching %s by ns=%s', tag, self.__ns)
elements = self._element.getElementsByTagNameNS(self.__ns, tag)
if not elements:
log.debug('searching %s', tag)
##log.debug('searching %s', tag)
elements = self._element.getElementsByTagName(tag)
if not elements:
#log.debug(self._element.toxml())
##log.debug(self._element.toxml())
if error:
raise AttributeError(u"No elements found")
raise AttributeError("No elements found")
else:
return
return SimpleXMLElement(
@@ -341,16 +229,17 @@ class SimpleXMLElement(object):
document=self.__document,
namespace=self.__ns,
prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map)
except AttributeError, e:
raise AttributeError(u"Tag not found: %s (%s)" % (tag, unicode(e)))
except AttributeError as e:
raise AttributeError("Tag not found: %s (%s)" % (tag, e))
def __getattr__(self, tag):
"Shortcut for __call__"
"""Shortcut for __call__"""
return self.__call__(tag)
def __iter__(self):
"Iterate over xml tags at this level"
"""Iterate over xml tags at this level"""
try:
for __element in self.__elements:
yield SimpleXMLElement(
@@ -358,67 +247,72 @@ class SimpleXMLElement(object):
document=self.__document,
namespace=self.__ns,
prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map)
except:
raise
def __dir__(self):
"List xml children tags names"
return [node.tagName for node
"""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]
"""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,
namespaces_map=self.__namespaces_map)
elements=elements,
document=self.__document,
namespace=self.__ns,
prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map
)
def __len__(self):
"Return elements count"
"""Return element count"""
return len(self.__elements)
def __contains__( self, item):
"Search for a tag name in this element or child nodes"
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"
"""Returns the unicode text nodes of the current element"""
if self._element.childNodes:
rc = u""
rc = ""
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")
"""Returns the str text nodes of the current element"""
return self.__unicode__()
def __int__(self):
"Returns the integer value of the current element"
"""Returns the integer value of the current element"""
return int(self.__str__())
def __float__(self):
"Returns the float value of the current element"
"""Returns the float value of the current element"""
try:
return float(self.__str__())
except:
raise IndexError(self._element.toxml())
raise IndexError(self._element.toxml())
_element = property(lambda self: self.__elements[0])
def unmarshall(self, types, strict=True):
"Convert to python values the current serialized xml element"
#import pdb; pdb.set_trace()
"""Convert to python values the current serialized xml element"""
# types is a dict of {tag name: convertion function}
# strict=False to use default type conversion if not specified
# example: types={'p': {'a': int,'b': int}, 'c': [{'d':str}]}
@@ -435,37 +329,62 @@ class SimpleXMLElement(object):
if ref_node['id'] == href:
node = ref_node
ref_name_type = ref_node['xsi:type'].split(":")[1]
break
break
try:
fn = types[name]
except (KeyError, ), e:
if node.get_namespace_uri("soapenc"):
fn = None # ignore multirefs!
elif 'xsi:type' in node.attributes().keys():
if isinstance(types, dict):
fn = types[name]
# custom array only in the response (not defined in the WSDL):
# <results soapenc:arrayType="xsd:string[199]>
if any([k for k,v in node[:] if 'arrayType' in k]) and not isinstance(fn, list):
fn = [fn]
else:
fn = types
except (KeyError, ) as e:
if 'xsi:type' in node.attributes().keys():
xsd_type = node['xsi:type'].split(":")[1]
fn = REVERSE_TYPE_MAP[xsd_type]
try:
fn = REVERSE_TYPE_MAP[xsd_type]
except:
fn = None # ignore multirefs!
elif strict:
raise TypeError(u"Tag: %s invalid (type not found)" % (name,))
raise TypeError("Tag: %s invalid (type not found)" % (name,))
else:
# if not strict, use default type conversion
fn = unicode
fn = str
if isinstance(fn, list):
# append to existing list (if any) - unnested dict arrays -
value = d.setdefault(name, [])
children = node.children()
for child in (children and children() or []): # Readability counts
value.append(child.unmarshall(fn[0], strict))
# TODO: check if this was really needed (get first child only)
##if len(fn[0]) == 1 and children:
## children = children()
if fn and not isinstance(fn[0], dict):
# simple arrays []
for child in (children or []):
tmp_dict = child.unmarshall(fn[0], strict)
value.extend(tmp_dict.values())
elif (self.__jetty and len(fn[0]) > 1):
# Jetty array style support [{k, v}]
for parent in node:
tmp_dict = {} # unmarshall each value & mix
for child in (node.children() or []):
tmp_dict.update(child.unmarshall(fn[0], strict))
value.append(tmp_dict)
else: # .Net / Java
for child in (children or []):
value.append(child.unmarshall(fn[0], strict))
elif isinstance(fn, tuple):
value = []
_d = {}
children = node.children()
as_dict = len(fn) == 1 and isinstance(fn[0], dict)
for child in (children and children() or []): # Readability counts
for child in (children and children() or []): # Readability counts
if as_dict:
_d.update(child.unmarshall(fn[0], strict)) # Merging pairs
_d.update(child.unmarshall(fn[0], strict)) # Merging pairs
else:
value.append(child.unmarshall(fn[0], strict))
if as_dict:
@@ -477,32 +396,32 @@ class SimpleXMLElement(object):
value = tuple(_tmp)
else:
value = tuple(value)
elif isinstance(fn, dict):
##if ref_name_type is not None:
## fn = fn[ref_name_type]
children = node.children()
value = children and children.unmarshall(fn, strict)
else:
if fn is None: # xsd:anyType not unmarshalled
if fn is None: # xsd:anyType not unmarshalled
value = node
elif str(node) or fn == str:
elif unicode(node) or (fn == str and unicode(node) != ''):
try:
# get special deserialization function (if any)
fn = TYPE_UNMARSHAL_FN.get(fn,fn)
fn = TYPE_UNMARSHAL_FN.get(fn, fn)
if fn == str:
# always return an unicode object:
# (avoid encoding errors in py<3!)
value = unicode(node)
else:
value = fn(unicode(node))
except (ValueError, TypeError), e:
raise ValueError(u"Tag: %s: %s" % (name, unicode(e)))
except (ValueError, TypeError) as e:
raise ValueError("Tag: %s: %s" % (name, e))
else:
value = None
d[name] = value
return d
def _update_ns(self, name):
"""Replace the defined namespace alias with tohse used by the client."""
pref = self.__ns_rx.search(name)
@@ -513,46 +432,50 @@ class SimpleXMLElement(object):
except KeyError:
log.warning('Unknown namespace alias %s' % name)
return name
def marshall(self, name, value, add_child=True, add_comments=False,
def marshall(self, name, value, add_child=True, add_comments=False,
ns=False, add_children_ns=True):
"Analize python value and add the serialized XML element using tag name"
"""Analyze python value and add the serialized XML element using tag name"""
# Change node name to that used by a client
name = self._update_ns(name)
if isinstance(value, dict): # serialize dict (<key>value</key>)
# for the first parent node, use the document target namespace
# (ns==True) or use the namespace string uri if passed (elements)
child = add_child and self.add_child(name, ns=ns) or self
for k,v in value.items():
for k, v in value.items():
if not add_children_ns:
ns = False
else:
# for children, use the wsdl element target namespace:
ns = getattr(value, 'namespace', None)
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
if not add_children_ns:
ns = False
for k,v in value:
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)
elif isinstance(value, list): # serialize lists
child = self.add_child(name, ns=ns)
if not add_children_ns:
ns = False
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?
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 = self.add_child(name, ns=ns)
child.add_comment(TYPE_MAP[value])
else: # the rest of object types are converted to string
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)
self.add_child(name, fn(value), ns=ns)
def import_node(self, other):
x = self.__document.importNode(other._element, True) # deep copy
+82 -50
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: latin-1 -*-
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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
@@ -10,30 +10,46 @@
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
"Pythonic simple SOAP Client implementation"
"""Pythonic simple SOAP Client transport"""
__author__ = "Mariano Reingart (reingart@gmail.com)"
__copyright__ = "Copyright (C) 2008 Mariano Reingart"
__license__ = "LGPL 3.0"
TIMEOUT = 60
import os
import cPickle as pickle
import urllib2
from urlparse import urlparse
import tempfile
from simplexml import SimpleXMLElement, TYPE_MAP, OrderedDict
import logging
import sys
try:
import urllib2
from cookielib import CookieJar
except ImportError:
from urllib import request as urllib2
from http.cookiejar import CookieJar
from . import __author__, __copyright__, __license__, __version__, TIMEOUT
from .simplexml import SimpleXMLElement, TYPE_MAP, OrderedDict
log = logging.getLogger(__name__)
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARNING)
#
# Socket wrapper to enable socket.TCP_NODELAY - this greatly speeds up transactions in Linux
# WARNING: this will modify the standard library socket module, use with care!
# TODO: implement this as a transport faciliy
# (to pass options directly to httplib2 or pycurl)
# be aware of metaclasses and socks.py (SocksiPy) used by httplib2
if False:
import socket
realsocket = socket.socket
def socketwrap(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
sockobj = realsocket(family, type, proto)
if type == socket.SOCK_STREAM:
sockobj.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
return sockobj
socket.socket = socketwrap
#
# We store metadata about what available transport mechanisms we have available.
#
_http_connectors = {} # libname: classimpl mapping
_http_facilities = {} # functionalitylabel: [sequence of libname] mapping
_http_connectors = {} # libname: classimpl mapping
_http_facilities = {} # functionalitylabel: [sequence of libname] mapping
class TransportBase:
@classmethod
@@ -45,27 +61,41 @@ class TransportBase:
#
try:
import httplib2
if sys.version > '3' and httplib2.__version__ <= "0.7.7":
import http.client
# httplib2 workaround: check_hostname needs a SSL context with either
# CERT_OPTIONAL or CERT_REQUIRED
# see https://code.google.com/p/httplib2/issues/detail?id=173
orig__init__ = http.client.HTTPSConnection.__init__
def fixer(self, host, port, key_file, cert_file, timeout, context,
check_hostname, *args, **kwargs):
chk = kwargs.get('disable_ssl_certificate_validation', True) ^ True
orig__init__(self, host, port=port, key_file=key_file,
cert_file=cert_file, timeout=timeout, context=context,
check_hostname=chk)
http.client.HTTPSConnection.__init__ = fixer
except ImportError:
TIMEOUT = None # timeout not supported by urllib2
TIMEOUT = None # timeout not supported by urllib2
pass
else:
class Httplib2Transport(httplib2.Http, TransportBase):
_wrapper_version = "httplib2 %s" % httplib2.__version__
_wrapper_name = 'httplib2'
def __init__(self, timeout, proxy=None, cacert=None, sessions=False):
##httplib2.debuglevel=4
kwargs = {}
if proxy:
import socks
kwargs['proxy_info'] = httplib2.ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, **proxy)
print "using proxy", proxy
log.info("using proxy %s" % proxy)
# set optional parameters according supported httplib2 version
if httplib2.__version__ >= '0.3.0':
kwargs['timeout'] = timeout
if httplib2.__version__ >= '0.7.0':
kwargs['disable_ssl_certificate_validation'] = cacert is None
kwargs['ca_certs'] = cacert
kwargs['ca_certs'] = cacert
httplib2.Http.__init__(self, **kwargs)
_http_connectors['httplib2'] = Httplib2Transport
@@ -76,15 +106,15 @@ else:
if 'timeout' in inspect.getargspec(httplib2.Http.__init__)[0]:
_http_facilities.setdefault('timeout', []).append('httplib2')
#
# urllib2 support.
#
import urllib2
class urllib2Transport(TransportBase):
_wrapper_version = "urllib2 %s" % urllib2.__version__
_wrapper_name = 'urllib2'
_wrapper_name = 'urllib2'
def __init__(self, timeout=None, proxy=None, cacert=None, sessions=False):
import sys
if (timeout is not None) and not self.supports_feature('timeout'):
raise RuntimeError('timeout is not supported with urllib2 transport')
if proxy:
@@ -94,26 +124,26 @@ class urllib2Transport(TransportBase):
self.request_opener = urllib2.urlopen
if sessions:
from cookielib import CookieJar
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
self.request_opener = opener.open
self._timeout = timeout
def request(self, url, method="GET", body=None, headers={}):
req = urllib2.Request(url, body, headers)
try:
f = self.request_opener(req, timeout=self._timeout)
except urllib2.HTTPError, f:
return f.info(), f.read()
except urllib2.HTTPError as f:
if f.code != 500:
raise
return f.info(), f.read()
return f.info(), f.read()
_http_connectors['urllib2'] = urllib2Transport
_http_facilities.setdefault('sessions', []).append('urllib2')
import sys
if sys.version_info >= (2,6):
if sys.version_info >= (2, 6):
_http_facilities.setdefault('timeout', []).append('urllib2')
del sys
@@ -129,19 +159,23 @@ else:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
class pycurlTransport(TransportBase):
_wrapper_version = pycurl.version
_wrapper_name = 'pycurl'
def __init__(self, timeout, proxy=None, cacert=None, sessions=False):
self.timeout = timeout
self.timeout = timeout
self.proxy = proxy or {}
self.cacert = cacert
def request(self, url, method, body, headers):
c = pycurl.Curl()
c.setopt(pycurl.URL, str(url))
c.setopt(pycurl.URL, url)
if 'proxy_host' in self.proxy:
c.setopt(pycurl.PROXY, self.proxy['proxy_host'])
if 'proxy_port' in self.proxy:
@@ -154,20 +188,19 @@ else:
#self.body = StringIO(body)
#c.setopt(pycurl.HEADERFUNCTION, self.header)
if self.cacert:
c.setopt(c.CAINFO, str(self.cacert))
c.setopt(c.CAINFO, self.cacert)
c.setopt(pycurl.SSL_VERIFYPEER, self.cacert and 1 or 0)
c.setopt(pycurl.SSL_VERIFYHOST, self.cacert and 2 or 0)
c.setopt(pycurl.CONNECTTIMEOUT, self.timeout/6)
c.setopt(pycurl.CONNECTTIMEOUT, self.timeout / 6)
c.setopt(pycurl.TIMEOUT, self.timeout)
if method=='POST':
if method == 'POST':
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, body)
c.setopt(pycurl.POSTFIELDS, body)
if headers:
hdrs = ['%s: %s' % (str(k), str(v)) for k, v in headers.items()]
##print hdrs
hdrs = ['%s: %s' % (k, v) for k, v in headers.items()]
log.debug(hdrs)
c.setopt(pycurl.HTTPHEADER, hdrs)
c.perform()
##print "pycurl perform..."
c.close()
return {}, self.buf.getvalue()
@@ -178,15 +211,15 @@ else:
class DummyTransport:
"Testing class to load a xml response"
"""Testing class to load a xml response"""
def __init__(self, xml_response):
self.xml_response = xml_response
def request(self, location, method, body, headers):
print method, location
print headers
print body
log.debug("%s %s", method, location)
log.debug(headers)
log.debug(body)
return {}, self.xml_response
@@ -222,20 +255,19 @@ def get_http_wrapper(library=None, features=[]):
else:
return _http_connectors[candidate_name]
def set_http_wrapper(library=None, features=[]):
"Set a suitable HTTP connection wrapper."
"""Set a suitable HTTP connection wrapper."""
global Http
Http = get_http_wrapper(library, features)
return Http
def get_Http():
"Return current transport class"
"""Return current transport class"""
global Http
return Http
# define the default HTTP connection class (it can be changed at runtime!):
set_http_wrapper()
+3 -2
View File
@@ -1,3 +1,4 @@
"""
Developed by Massimo Di Pierro
Released under the web2py license (LGPL)
@@ -105,9 +106,9 @@ class WebClient(object):
# assume everything is ok and make http request
error = None
try:
if isinstance(data,str):
if isinstance(data, str):
self.method = 'POST' if method=='auto' else method
if isinstance(data, dict):
elif isinstance(data, dict):
self.method = 'POST' if method=='auto' else method
# if there is only one form, set _formname automatically
if not '_formname' in data and len(self.forms) == 1:
+32
View File
@@ -40,6 +40,7 @@ def fix_sys_path():
fix_sys_path()
from contrib.webclient import WebClient
from urllib2 import HTTPError
webserverprocess = None
@@ -140,6 +141,37 @@ class TestWeb(LiveTest):
assert('expires' in s.headers)
assert(s.headers['cache-control'].startswith('max-age'))
def testSoap(self):
# test soap server implementation
from gluon.contrib.pysimplesoap.client import SoapClient, SoapFault
url = 'http://127.0.0.1:8000/examples/soap_examples/call/soap?WSDL'
client = SoapClient(wsdl=url)
ret = client.SubIntegers(a=3, b=2)
# check that the value returned is ok
assert('SubResult' in ret)
assert(ret['SubResult'] == 1)
try:
ret = client.Division(a=3, b=0)
except SoapFault, sf:
# verify the exception value is ok
assert(sf.faultstring == "float division by zero")
assert(sf.faultcode == "Server.ZeroDivisionError")
# store sent and received xml for low level test
xml_request = client.xml_request
xml_response = client.xml_response
# do a low level raw soap request (using
s = WebClient('http://127.0.0.1:8000/')
try:
s.post('examples/soap_examples/call/soap', data=xml_request, method="POST")
except HTTPError, e:
assert(e.msg=='INTERNAL SERVER ERROR')
# check internal server error returned (issue 153)
assert(s.status == 500)
assert(s.text == xml_response)
if __name__ == '__main__':
unittest.main()
+7 -1
View File
@@ -4830,9 +4830,15 @@ class Service(object):
for method, (function, returns, args, doc) in procedures.iteritems():
dispatcher.register_function(method, function, returns, args, doc)
if request.env.request_method == 'POST':
fault = {}
# Process normal Soap Operation
response.headers['Content-Type'] = 'text/xml'
return dispatcher.dispatch(request.body.read())
xml = dispatcher.dispatch(request.body.read(), fault=fault)
if fault:
# May want to consider populating a ticket here...
response.status = 500
# return the soap response
return xml
elif 'WSDL' in request.vars:
# Return Web Service Description
response.headers['Content-Type'] = 'text/xml'