updated simple soap contrib (pysimplesoap version 1.11)

This commit is contained in:
Mariano Reingart
2014-01-19 16:57:08 -03:00
parent e10fd68f90
commit 3df446c306
6 changed files with 1448 additions and 940 deletions
+14 -5
View File
@@ -1,7 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"PySimpleSOAP"
import client """PySimpleSOAP"""
import server
import simplexml
import transport __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
+189 -99
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/python
# -*- coding: latin-1 -*- # -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify # 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 # 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 # Free Software Foundation; either version 3, or (at your option) any later
@@ -10,26 +10,37 @@
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details. # 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 logging
import warnings
import re import re
import traceback 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__) log = logging.getLogger(__name__)
# Deprecated # Deprecated?
DEBUG = False NS_RX = re.compile(r'xmlns:(\w+)="(.+?)"')
NS_RX=re.compile(r'xmlns:(\w+)="(.+?)"')
class SoapDispatcher(object): class SoapDispatcher(object):
"Simple Dispatcher for SOAP Server" """Simple Dispatcher for SOAP Server"""
def __init__(self, name, documentation='', action='', location='', def __init__(self, name, documentation='', action='', location='',
namespace=None, prefix=False, namespace=None, prefix=False,
@@ -79,9 +90,9 @@ class SoapDispatcher(object):
self.methods = {} self.methods = {}
self.name = name self.name = name
self.documentation = documentation self.documentation = documentation
self.action = action # base SoapAction self.action = action # base SoapAction
self.location = location self.location = location
self.namespace = namespace # targetNamespace self.namespace = namespace # targetNamespace
self.prefix = prefix self.prefix = prefix
self.soap_ns = soap_ns self.soap_ns = soap_ns
self.soap_uri = soap_uri self.soap_uri = soap_uri
@@ -89,7 +100,6 @@ class SoapDispatcher(object):
self.pretty = pretty self.pretty = pretty
self.debug = debug self.debug = debug
@staticmethod @staticmethod
def _extra_namespaces(xml, ns): def _extra_namespaces(xml, ns):
"""Extends xml with extra namespaces. """Extends xml with extra namespaces.
@@ -99,25 +109,26 @@ class SoapDispatcher(object):
if ns: if ns:
_tpl = 'xmlns:%s="%s"' _tpl = 'xmlns:%s="%s"'
_ns_str = " ".join([_tpl % (prefix, uri) for uri, prefix in ns.items() if uri not in xml]) _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 return xml
def register_function(self, name, fn, returns=None, args=None, doc=None): def register_function(self, name, fn, returns=None, args=None, doc=None):
self.methods[name] = fn, returns, args, doc or getattr(fn, "__doc__", "") self.methods[name] = fn, returns, args, doc or getattr(fn, "__doc__", "")
def dispatch(self, xml, action=None, fault=None):
def dispatch(self, xml, action=None): """Receive and process SOAP call, returns the xml"""
"Receive and proccess SOAP call" # a dict can be sent in fault to expose it to the caller
# default values: # default values:
prefix = self.prefix prefix = self.prefix
ret = fault = None ret = None
if fault is None:
fault = {}
soap_ns, soap_uri = self.soap_ns, self.soap_uri soap_ns, soap_uri = self.soap_ns, self.soap_uri
soap_fault_code = 'VersionMismatch' soap_fault_code = 'VersionMismatch'
name = None name = None
# namespaces = [('model', 'http://model.common.mt.moboperator'), ('external', 'http://external.mt.moboperator')] # 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'} # _ns_reversed = {'http://external.mt.moboperator': 'external', 'http://model.common.mt.moboperator': 'model'}
try: try:
@@ -126,7 +137,7 @@ class SoapDispatcher(object):
# detect soap prefix and uri (xmlns attributes of Envelope) # detect soap prefix and uri (xmlns attributes of Envelope)
for k, v in request[:]: for k, v in request[:]:
if v in ("http://schemas.xmlsoap.org/soap/envelope/", 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_ns = request.attributes()[k].localName
soap_uri = request.attributes()[k].value soap_uri = request.attributes()[k].value
@@ -134,7 +145,7 @@ class SoapDispatcher(object):
elif v in self.namespaces.values(): elif v in self.namespaces.values():
_ns = request.attributes()[k].localName _ns = request.attributes()[k].localName
_uri = request.attributes()[k].value _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' # 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 # After that we know how the client has prefixed additional namespaces
@@ -164,16 +175,16 @@ class SoapDispatcher(object):
if args_types: if args_types:
args = method.children().unmarshall(args_types) args = method.children().unmarshall(args_types)
elif args_types is None: elif args_types is None:
args = {'request': method} # send raw request args = {'request': method} # send raw request
else: else:
args = {} # no parameters args = {} # no parameters
soap_fault_code = 'Server' soap_fault_code = 'Server'
# execute function # execute function
ret = function(**args) ret = function(**args)
log.debug('dispathed method returns: %s', ret) 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 import sys
etype, evalue, etb = sys.exc_info() etype, evalue, etb = sys.exc_info()
log.error(traceback.format_exc()) log.error(traceback.format_exc())
@@ -182,9 +193,9 @@ class SoapDispatcher(object):
detail += '\n\nXML REQUEST\n\n' + xml detail += '\n\nXML REQUEST\n\n' + xml
else: else:
detail = None detail = None
fault = {'faultcode': "%s.%s" % (soap_fault_code, etype.__name__), fault.update({'faultcode': "%s.%s" % (soap_fault_code, etype.__name__),
'faultstring': unicode(evalue), 'faultstring': evalue,
'detail': detail} 'detail': detail})
# build response message # build response message
if not prefix: if not prefix:
@@ -206,12 +217,12 @@ class SoapDispatcher(object):
# Change our namespace alias to that given by the client. # Change our namespace alias to that given by the client.
# We put [('model', 'http://model.common.mt.moboperator'), ('external', 'http://external.mt.moboperator')] # 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'} # 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'} # and get {'model': u'mod', 'external': u'ext'}
response = SimpleXMLElement(xml, response = SimpleXMLElement(xml,
namespace=self.namespace, namespace=self.namespace,
namespaces_map = mapping, namespaces_map=mapping,
prefix=prefix) prefix=prefix)
response['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance" response['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
@@ -226,14 +237,24 @@ class SoapDispatcher(object):
# return normal value # return normal value
res = body.add_child("%sResponse" % name, ns=prefix) res = body.add_child("%sResponse" % name, ns=prefix)
if not 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 # serialize returned values (response) if type definition available
if returns_types: 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, ) res.marshall(returns_types.keys()[0], ret, )
else: else:
for k,v in ret.items(): # new style for complex classes
for k, v in ret.items():
res.marshall(k, v) res.marshall(k, v)
elif returns_types is None: elif returns_types is None:
# merge xmlelement returned # merge xmlelement returned
@@ -246,16 +267,16 @@ class SoapDispatcher(object):
# Introspection functions: # Introspection functions:
def list_methods(self): def list_methods(self):
"Return a list of aregistered operations" """Return a list of aregistered operations"""
return [(method, doc) for method, (function, returns, args, doc) in self.methods.items()] return [(method, doc) for method, (function, returns, args, doc) in self.methods.items()]
def help(self, method=None): def help(self, method=None):
"Generate sample request and response messages" """Generate sample request and response messages"""
(function, returns, args, doc) = self.methods[method] (function, returns, args, doc) = self.methods[method]
xml = """ xml = """
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body><%(method)s xmlns="%(namespace)s"/></soap:Body> <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) request = SimpleXMLElement(xml, namespace=self.namespace, prefix=self.prefix)
if args: if args:
items = args.items() items = args.items()
@@ -263,13 +284,13 @@ class SoapDispatcher(object):
items = [('value', None)] items = [('value', None)]
else: else:
items = [] items = []
for k,v in items: for k, v in items:
request(method).marshall(k, v, add_comments=True, ns=False) request(method).marshall(k, v, add_comments=True, ns=False)
xml = """ xml = """
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body><%(method)sResponse xmlns="%(namespace)s"/></soap:Body> <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) response = SimpleXMLElement(xml, namespace=self.namespace, prefix=self.prefix)
if returns: if returns:
items = returns.items() items = returns.items()
@@ -277,14 +298,13 @@ class SoapDispatcher(object):
items = [('value', None)] items = [('value', None)]
else: else:
items = [] items = []
for k,v in items: for k, v in items:
response('%sResponse'%method).marshall(k, v, add_comments=True, ns=False) response('%sResponse' % method).marshall(k, v, add_comments=True, ns=False)
return request.as_xml(pretty=True), response.as_xml(pretty=True), doc return request.as_xml(pretty=True), response.as_xml(pretty=True), doc
def wsdl(self): def wsdl(self):
"Generate Web Service Description v1.1" """Generate Web Service Description v1.1"""
xml = """<?xml version="1.0"?> xml = """<?xml version="1.0"?>
<wsdl:definitions name="%(name)s" <wsdl:definitions name="%(name)s"
targetNamespace="%(namespace)s" targetNamespace="%(namespace)s"
@@ -326,38 +346,40 @@ class SoapDispatcher(object):
all = complex.add_child("xsd:all") all = complex.add_child("xsd:all")
elif items: elif items:
all = complex.add_child("xsd:sequence") all = complex.add_child("xsd:sequence")
for k,v in items: for k, v in items:
e = all.add_child("xsd:element") e = all.add_child("xsd:element")
e['name'] = k e['name'] = k
if array: if array:
e[:]={'minOccurs': "0", 'maxOccurs': "unbounded"} e[:] = {'minOccurs': "0", 'maxOccurs': "unbounded"}
if v in TYPE_MAP.keys(): if v in TYPE_MAP.keys():
t='xsd:%s' % TYPE_MAP[v] t = 'xsd:%s' % TYPE_MAP[v]
elif v is None: elif v is None:
t='xsd:anyType' t = 'xsd:anyType'
elif isinstance(v, list): elif isinstance(v, list):
n="ArrayOf%s%s" % (name, k) n = "ArrayOf%s%s" % (name, k)
l = [] l = []
for d in v: for d in v:
l.extend(d.items()) l.extend(d.items())
parse_element(n, l, array=True, complex=True) parse_element(n, l, array=True, complex=True)
t = "tns:%s" % n t = "tns:%s" % n
elif isinstance(v, dict): elif isinstance(v, dict):
n="%s%s" % (name, k) n = "%s%s" % (name, k)
parse_element(n, v.items(), complex=True) parse_element(n, v.items(), complex=True)
t = "tns:%s" % n t = "tns:%s" % n
else:
raise TypeError("unknonw type v for marshalling" % str(v))
e.add_attribute('type', t) e.add_attribute('type', t)
parse_element("%s" % method, args and args.items()) parse_element("%s" % method, args and args.items())
parse_element("%sResponse" % method, returns and returns.items()) parse_element("%sResponse" % method, returns and returns.items())
# create messages: # create messages:
for m,e in ('Input',''), ('Output','Response'): for m, e in ('Input', ''), ('Output', 'Response'):
message = wsdl.add_child('wsdl:message') message = wsdl.add_child('wsdl:message')
message['name'] = "%s%s" % (method, m) message['name'] = "%s%s" % (method, m)
part = message.add_child("wsdl:part") part = message.add_child("wsdl:part")
part[:] = {'name': 'parameters', part[:] = {'name': 'parameters',
'element': 'tns:%s%s' % (method,e)} 'element': 'tns:%s%s' % (method, e)}
# create ports # create ports
portType = wsdl.add_child('wsdl:portType') portType = wsdl.add_child('wsdl:portType')
@@ -397,7 +419,7 @@ class SoapDispatcher(object):
service = wsdl.add_child('wsdl:service') service = wsdl.add_child('wsdl:service')
service["name"] = "%sService" % self.name service["name"] = "%sService" % self.name
service.add_child('wsdl:documentation', text=self.documentation) 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["name"] = "%s" % self.name
port["binding"] = "tns:%sBinding" % self.name port["binding"] = "tns:%sBinding" % self.name
soapaddress = port.add_child('soap:address') soapaddress = port.add_child('soap:address')
@@ -405,10 +427,10 @@ class SoapDispatcher(object):
return wsdl.as_xml(pretty=True) return wsdl.as_xml(pretty=True)
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class SOAPHandler(BaseHTTPRequestHandler): class SOAPHandler(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
"User viewable help information and wsdl" """User viewable help information and wsdl"""
args = self.path[1:].split("?") args = self.path[1:].split("?")
if self.path != "/" and args[0] not in self.server.dispatcher.methods.keys(): if self.path != "/" and args[0] not in self.server.dispatcher.methods.keys():
self.send_error(404, "Method not found: %s" % args[0]) self.send_error(404, "Method not found: %s" % args[0])
@@ -419,7 +441,7 @@ class SOAPHandler(BaseHTTPRequestHandler):
else: else:
# return supplied method help (?request or ?response messages) # return supplied method help (?request or ?response messages)
req, res, doc = self.server.dispatcher.help(args[0]) 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 response = req
else: else:
response = res response = res
@@ -429,82 +451,150 @@ class SOAPHandler(BaseHTTPRequestHandler):
self.wfile.write(response) self.wfile.write(response)
def do_POST(self): def do_POST(self):
"SOAP POST gateway" """SOAP POST gateway"""
self.send_response(200) 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.send_header("Content-type", "text/xml")
self.end_headers() self.end_headers()
request = self.rfile.read(int(self.headers.getheader('content-length')))
response = self.server.dispatcher.dispatch(request)
self.wfile.write(response) self.wfile.write(response)
if __name__=="__main__": class WSGISOAPHandler(object):
import sys
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( dispatcher = SoapDispatcher(
name = "PySimpleSoapSample", name="PySimpleSoapSample",
location = "http://localhost:8008/", location="http://localhost:8008/",
action = 'http://localhost:8008/', # SOAPAction action='http://localhost:8008/', # SOAPAction
namespace = "http://example.com/pysimplesoapsamle/", prefix="ns0", namespace="http://example.com/pysimplesoapsamle/", prefix="ns0",
documentation = 'Example soap service using PySimpleSoap', documentation='Example soap service using PySimpleSoap',
trace = True, trace=True, debug=True,
ns = True) ns=True)
def adder(p,c, dt=None): def adder(p, c, dt=None):
"Add several values" """Add several values"""
print c[0]['d'],c[1]['d'],
import datetime
dt = dt + datetime.timedelta(365) 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): def dummy(in0):
"Just return input" """Just return input"""
return in0 return in0
def echo(request): def echo(request):
"Copy request->response (generic, any type)" """Copy request->response (generic, any type)"""
return request.value return request.value
dispatcher.register_function('Adder', adder, dispatcher.register_function(
returns={'AddResult': {'ab': int, 'dd': str } }, 'Adder', adder,
args={'p': {'a': int,'b': int}, 'dt': Date, 'c': [{'d': Decimal}]}) 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, dispatcher.register_function(
'Dummy', dummy,
returns={'out0': str}, returns={'out0': str},
args={'in0': str}) args={'in0': str}
)
dispatcher.register_function('Echo', echo) dispatcher.register_function('Echo', echo)
if '--local' in sys.argv: if '--local' in sys.argv:
wsdl=dispatcher.wsdl() wsdl = dispatcher.wsdl()
print wsdl
# Commented because path is platform dependent
# Looks that it doesnt matter.
# open("C:/test.wsdl","w").write(wsdl)
for method, doc in dispatcher.list_methods(): for method, doc in dispatcher.list_methods():
request, response, doc = dispatcher.help(method) request, response, doc = dispatcher.help(method)
##print request
##print response
if '--serve' in sys.argv: if '--serve' in sys.argv:
print "Starting server..." log.info("Starting server...")
httpd = HTTPServer(("", 8008), SOAPHandler) httpd = HTTPServer(("", 8008), SOAPHandler)
httpd.dispatcher = dispatcher httpd.dispatcher = dispatcher
httpd.serve_forever() 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: if '--consume' in sys.argv:
from client import SoapClient from .client import SoapClient
client = SoapClient( client = SoapClient(
location = "http://localhost:8008/", location="http://localhost:8008/",
action = 'http://localhost:8008/', # SOAPAction action='http://localhost:8008/', # SOAPAction
namespace = "http://example.com/sample.wsdl", namespace="http://example.com/sample.wsdl",
soap_ns='soap', soap_ns='soap',
trace = True, trace=True,
ns = False) ns=False
response = client.Adder(p={'a':1,'b':2},dt='20100724',c=[{'d':'1.20'},{'d':'2.01'}]) )
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 result = response.AddResult
print int(result.ab) log.info(int(result.ab))
print str(result.dd) 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']))
+161 -238
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/python
# -*- coding: latin-1 -*- # -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify # 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 # 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 # Free Software Foundation; either version 3, or (at your option) any later
@@ -10,163 +10,46 @@
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details. # 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 logging
import re import re
import time import time
import warnings
import xml.dom.minidom 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__) 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): class SimpleXMLElement(object):
"Simple XML manipulation (simil PHP)" """Simple XML manipulation (simil PHP)"""
def __init__(self, text = None, elements = None, document = None, def __init__(self, text=None, elements=None, document=None,
namespace = None, prefix=None, namespaces_map={}): namespace=None, prefix=None, namespaces_map={}, jetty=False):
""" """
:param namespaces_map: How to map our namespace prefix to that given by the client; :param namespaces_map: How to map our namespace prefix to that given by the client;
{prefix: received_prefix} {prefix: received_prefix}
""" """
self.__namespaces_map = namespaces_map self.__namespaces_map = namespaces_map
_rx = "|".join(namespaces_map.keys()) # {'external': 'ext', 'model': 'mod'} -> 'external|model' _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):.*$ 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> # to find prefixes in all xml nodes i.e.: <model:code>1</model:code>
# and later change that to <mod:code>1</mod:code> # and later change that to <mod:code>1</mod:code>
self.__ns = namespace self.__ns = namespace
self.__prefix = prefix self.__prefix = prefix
self.__jetty = jetty # special list support
if text is not None: if text is not None:
try: try:
@@ -180,76 +63,79 @@ class SimpleXMLElement(object):
self.__document = document self.__document = document
def add_child(self, name, text=None, ns=True): def add_child(self, name, text=None, ns=True):
"Adding a child tag to a node" """Adding a child tag to a node"""
if not ns or not self.__ns: if not ns or self.__ns is False:
log.debug('adding %s', name) ##log.debug('adding %s without namespace', name)
element = self.__document.createElement(name) element = self.__document.createElement(name)
else: else:
log.debug('adding %s ns "%s" %s', name, self.__ns, ns) ##log.debug('adding %s ns "%s" %s', name, self.__ns, ns)
if self.__prefix: 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)) element = self.__document.createElementNS(self.__ns, "%s:%s" % (self.__prefix, name))
else: else:
element = self.__document.createElementNS(self.__ns, name) element = self.__document.createElementNS(self.__ns, name)
# don't append null tags! # don't append null tags!
if text is not None: if text is not None:
if isinstance(text, unicode): element.appendChild(self.__document.createTextNode(text))
element.appendChild(self.__document.createTextNode(text))
else:
element.appendChild(self.__document.createTextNode(str(text)))
self._element.appendChild(element) self._element.appendChild(element)
return SimpleXMLElement( return SimpleXMLElement(
elements=[element], elements=[element],
document=self.__document, document=self.__document,
namespace=self.__ns, namespace=self.__ns,
prefix=self.__prefix, prefix=self.__prefix,
namespaces_map=self.__namespaces_map) jetty=self.__jetty,
namespaces_map=self.__namespaces_map
)
def __setattr__(self, tag, text): def __setattr__(self, tag, text):
"Add text child tag node (short form)" """Add text child tag node (short form)"""
if tag.startswith("_"): if tag.startswith("_"):
object.__setattr__(self, tag, text) object.__setattr__(self, tag, text)
else: else:
log.debug('__setattr__(%s, %s)', tag, text) ##log.debug('__setattr__(%s, %s)', tag, text)
self.add_child(tag, text) self.add_child(tag, text)
def __delattr__(self, tag): def __delattr__(self, tag):
"Remove a child tag (non recursive!)" """Remove a child tag (non recursive!)"""
elements=[__element for __element in self._element.childNodes elements = [__element for __element in self._element.childNodes
if __element.nodeType == __element.ELEMENT_NODE if __element.nodeType == __element.ELEMENT_NODE]
]
for element in elements: for element in elements:
self._element.removeChild(element) self._element.removeChild(element)
def add_comment(self, data): def add_comment(self, data):
"Add an xml comment to this child" """Add an xml comment to this child"""
comment = self.__document.createComment(data) comment = self.__document.createComment(data)
self._element.appendChild(comment) self._element.appendChild(comment)
def as_xml(self, filename=None, pretty=False): 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: if not pretty:
return self.__document.toxml('UTF-8') return self.__document.toxml('UTF-8')
else: else:
return self.__document.toprettyxml(encoding='UTF-8') return self.__document.toprettyxml(encoding='UTF-8')
def __repr__(self): 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') return self._element.toxml('UTF-8')
def get_name(self): def get_name(self):
"Return the tag name of this node" """Return the tag name of this node"""
return self._element.tagName return self._element.tagName
def get_local_name(self): 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 return self._element.localName
def get_prefix(self): def get_prefix(self):
"Return the namespace prefix of this node" """Return the namespace prefix of this node"""
return self._element.prefix return self._element.prefix
def get_namespace_uri(self, ns): def get_namespace_uri(self, ns):
"Return the namespace uri for a prefix" """Return the namespace uri for a prefix"""
element = self._element element = self._element
while element is not None and element.attributes is not None: while element is not None and element.attributes is not None:
try: try:
@@ -257,38 +143,39 @@ class SimpleXMLElement(object):
except KeyError: except KeyError:
element = element.parentNode element = element.parentNode
def attributes(self): def attributes(self):
"Return a dict of attributes for this tag" """Return a dict of attributes for this tag"""
#TODO: use slice syntax [:]? #TODO: use slice syntax [:]?
return self._element.attributes return self._element.attributes
def __getitem__(self, item): def __getitem__(self, item):
"Return xml tag attribute value or a slice of attributes (iter)" """Return xml tag attribute value or a slice of attributes (iter)"""
log.debug('__getitem__(%s)', item) ##log.debug('__getitem__(%s)', item)
if isinstance(item, basestring): if isinstance(item, basestring):
if self._element.hasAttribute(item): if self._element.hasAttribute(item):
return self._element.attributes[item].value return self._element.attributes[item].value
elif isinstance(item, slice): elif isinstance(item, slice):
# return a list with name:values # return a list with name:values
return self._element.attributes.items()[item] return list(self._element.attributes.items())[item]
else: else:
# return element by index (position) # return element by index (position)
element = self.__elements[item] element = self.__elements[item]
return SimpleXMLElement( return SimpleXMLElement(
elements=[element], elements=[element],
document=self.__document, document=self.__document,
namespace=self.__ns, namespace=self.__ns,
prefix=self.__prefix, prefix=self.__prefix,
namespaces_map=self.__namespaces_map) jetty=self.__jetty,
namespaces_map=self.__namespaces_map
)
def add_attribute(self, name, value): 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) self._element.setAttribute(name, value)
def __setitem__(self, item, value): def __setitem__(self, item, value):
"Set an attribute value" """Set an attribute value"""
if isinstance(item,basestring): if isinstance(item, basestring):
self.add_attribute(item, value) self.add_attribute(item, value)
elif isinstance(item, slice): elif isinstance(item, slice):
# set multiple attributes at once # set multiple attributes at once
@@ -297,7 +184,7 @@ class SimpleXMLElement(object):
def __call__(self, tag=None, ns=None, children=False, root=False, def __call__(self, tag=None, ns=None, children=False, root=False,
error=True, ): 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: try:
if root: if root:
# return entire document # return entire document
@@ -306,6 +193,7 @@ class SimpleXMLElement(object):
document=self.__document, document=self.__document,
namespace=self.__ns, namespace=self.__ns,
prefix=self.__prefix, prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map namespaces_map=self.__namespaces_map
) )
if tag is None: if tag is None:
@@ -317,23 +205,23 @@ class SimpleXMLElement(object):
elements = None elements = None
if isinstance(tag, int): if isinstance(tag, int):
# return tag by index # return tag by index
elements=[self.__elements[tag]] elements = [self.__elements[tag]]
if ns and not elements: if ns and not elements:
for ns_uri in isinstance(ns, (tuple, list)) and ns or (ns, ): 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) elements = self._element.getElementsByTagNameNS(ns_uri, tag)
if elements: if elements:
break break
if self.__ns and not elements: 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) elements = self._element.getElementsByTagNameNS(self.__ns, tag)
if not elements: if not elements:
log.debug('searching %s', tag) ##log.debug('searching %s', tag)
elements = self._element.getElementsByTagName(tag) elements = self._element.getElementsByTagName(tag)
if not elements: if not elements:
#log.debug(self._element.toxml()) ##log.debug(self._element.toxml())
if error: if error:
raise AttributeError(u"No elements found") raise AttributeError("No elements found")
else: else:
return return
return SimpleXMLElement( return SimpleXMLElement(
@@ -341,16 +229,17 @@ class SimpleXMLElement(object):
document=self.__document, document=self.__document,
namespace=self.__ns, namespace=self.__ns,
prefix=self.__prefix, prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map) namespaces_map=self.__namespaces_map)
except AttributeError, e: except AttributeError as e:
raise AttributeError(u"Tag not found: %s (%s)" % (tag, unicode(e))) raise AttributeError("Tag not found: %s (%s)" % (tag, e))
def __getattr__(self, tag): def __getattr__(self, tag):
"Shortcut for __call__" """Shortcut for __call__"""
return self.__call__(tag) return self.__call__(tag)
def __iter__(self): def __iter__(self):
"Iterate over xml tags at this level" """Iterate over xml tags at this level"""
try: try:
for __element in self.__elements: for __element in self.__elements:
yield SimpleXMLElement( yield SimpleXMLElement(
@@ -358,42 +247,45 @@ class SimpleXMLElement(object):
document=self.__document, document=self.__document,
namespace=self.__ns, namespace=self.__ns,
prefix=self.__prefix, prefix=self.__prefix,
jetty=self.__jetty,
namespaces_map=self.__namespaces_map) namespaces_map=self.__namespaces_map)
except: except:
raise raise
def __dir__(self): def __dir__(self):
"List xml children tags names" """List xml children tags names"""
return [node.tagName for node return [node.tagName for node
in self._element.childNodes in self._element.childNodes
if node.nodeType != node.TEXT_NODE] if node.nodeType != node.TEXT_NODE]
def children(self): def children(self):
"Return xml children tags element" """Return xml children tags element"""
elements=[__element for __element in self._element.childNodes elements = [__element for __element in self._element.childNodes
if __element.nodeType == __element.ELEMENT_NODE] if __element.nodeType == __element.ELEMENT_NODE]
if not elements: if not elements:
return None return None
#raise IndexError("Tag %s has no children" % self._element.tagName) #raise IndexError("Tag %s has no children" % self._element.tagName)
return SimpleXMLElement( return SimpleXMLElement(
elements=elements, elements=elements,
document=self.__document, document=self.__document,
namespace=self.__ns, namespace=self.__ns,
prefix=self.__prefix, prefix=self.__prefix,
namespaces_map=self.__namespaces_map) jetty=self.__jetty,
namespaces_map=self.__namespaces_map
)
def __len__(self): def __len__(self):
"Return elements count" """Return element count"""
return len(self.__elements) return len(self.__elements)
def __contains__( self, item): def __contains__(self, item):
"Search for a tag name in this element or child nodes" """Search for a tag name in this element or child nodes"""
return self._element.getElementsByTagName(item) return self._element.getElementsByTagName(item)
def __unicode__(self): 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: if self._element.childNodes:
rc = u"" rc = ""
for node in self._element.childNodes: for node in self._element.childNodes:
if node.nodeType == node.TEXT_NODE: if node.nodeType == node.TEXT_NODE:
rc = rc + node.data rc = rc + node.data
@@ -401,15 +293,15 @@ class SimpleXMLElement(object):
return '' return ''
def __str__(self): def __str__(self):
"Returns the str text nodes of the current element" """Returns the str text nodes of the current element"""
return unicode(self).encode("utf8","ignore") return self.__unicode__()
def __int__(self): def __int__(self):
"Returns the integer value of the current element" """Returns the integer value of the current element"""
return int(self.__str__()) return int(self.__str__())
def __float__(self): def __float__(self):
"Returns the float value of the current element" """Returns the float value of the current element"""
try: try:
return float(self.__str__()) return float(self.__str__())
except: except:
@@ -418,7 +310,9 @@ class SimpleXMLElement(object):
_element = property(lambda self: self.__elements[0]) _element = property(lambda self: self.__elements[0])
def unmarshall(self, types, strict=True): 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} # types is a dict of {tag name: convertion function}
# strict=False to use default type conversion if not specified # strict=False to use default type conversion if not specified
# example: types={'p': {'a': int,'b': int}, 'c': [{'d':str}]} # example: types={'p': {'a': int,'b': int}, 'c': [{'d':str}]}
@@ -436,26 +330,51 @@ class SimpleXMLElement(object):
node = ref_node node = ref_node
ref_name_type = ref_node['xsi:type'].split(":")[1] ref_name_type = ref_node['xsi:type'].split(":")[1]
break break
try: try:
fn = types[name] if isinstance(types, dict):
except (KeyError, ), e: fn = types[name]
if node.get_namespace_uri("soapenc"): # custom array only in the response (not defined in the WSDL):
fn = None # ignore multirefs! # <results soapenc:arrayType="xsd:string[199]>
elif 'xsi:type' in node.attributes().keys(): 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] 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: elif strict:
raise TypeError(u"Tag: %s invalid (type not found)" % (name,)) raise TypeError("Tag: %s invalid (type not found)" % (name,))
else: else:
# if not strict, use default type conversion # if not strict, use default type conversion
fn = unicode fn = str
if isinstance(fn, list): if isinstance(fn, list):
# append to existing list (if any) - unnested dict arrays - # append to existing list (if any) - unnested dict arrays -
value = d.setdefault(name, []) value = d.setdefault(name, [])
children = node.children() children = node.children()
for child in (children and children() or []): # Readability counts # TODO: check if this was really needed (get first child only)
value.append(child.unmarshall(fn[0], strict)) ##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): elif isinstance(fn, tuple):
value = [] value = []
@@ -463,9 +382,9 @@ class SimpleXMLElement(object):
children = node.children() children = node.children()
as_dict = len(fn) == 1 and isinstance(fn[0], dict) 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: if as_dict:
_d.update(child.unmarshall(fn[0], strict)) # Merging pairs _d.update(child.unmarshall(fn[0], strict)) # Merging pairs
else: else:
value.append(child.unmarshall(fn[0], strict)) value.append(child.unmarshall(fn[0], strict))
if as_dict: if as_dict:
@@ -484,25 +403,25 @@ class SimpleXMLElement(object):
children = node.children() children = node.children()
value = children and children.unmarshall(fn, strict) value = children and children.unmarshall(fn, strict)
else: else:
if fn is None: # xsd:anyType not unmarshalled if fn is None: # xsd:anyType not unmarshalled
value = node value = node
elif str(node) or fn == str: elif unicode(node) or (fn == str and unicode(node) != ''):
try: try:
# get special deserialization function (if any) # get special deserialization function (if any)
fn = TYPE_UNMARSHAL_FN.get(fn,fn) fn = TYPE_UNMARSHAL_FN.get(fn, fn)
if fn == str: if fn == str:
# always return an unicode object: # always return an unicode object:
# (avoid encoding errors in py<3!)
value = unicode(node) value = unicode(node)
else: else:
value = fn(unicode(node)) value = fn(unicode(node))
except (ValueError, TypeError), e: except (ValueError, TypeError) as e:
raise ValueError(u"Tag: %s: %s" % (name, unicode(e))) raise ValueError("Tag: %s: %s" % (name, e))
else: else:
value = None value = None
d[name] = value d[name] = value
return d return d
def _update_ns(self, name): def _update_ns(self, name):
"""Replace the defined namespace alias with tohse used by the client.""" """Replace the defined namespace alias with tohse used by the client."""
pref = self.__ns_rx.search(name) pref = self.__ns_rx.search(name)
@@ -514,42 +433,46 @@ class SimpleXMLElement(object):
log.warning('Unknown namespace alias %s' % name) log.warning('Unknown namespace alias %s' % name)
return 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): 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 # Change node name to that used by a client
name = self._update_ns(name) name = self._update_ns(name)
if isinstance(value, dict): # serialize dict (<key>value</key>) 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 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: if not add_children_ns:
ns = False 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) child.marshall(k, v, add_comments=add_comments, ns=ns)
elif isinstance(value, tuple): # serialize tuple (<key>value</key>) elif isinstance(value, tuple): # serialize tuple (<key>value</key>)
child = add_child and self.add_child(name, ns=ns) or self child = add_child and self.add_child(name, ns=ns) or self
if not add_children_ns: if not add_children_ns:
ns = False ns = False
for k,v in value: for k, v in value:
getattr(self, name).marshall(k, v, add_comments=add_comments, ns=ns) getattr(self, name).marshall(k, v, add_comments=add_comments, ns=ns)
elif isinstance(value, list): # serialize lists elif isinstance(value, list): # serialize lists
child=self.add_child(name, ns=ns) child = self.add_child(name, ns=ns)
if not add_children_ns: if not add_children_ns:
ns = False ns = False
if add_comments: if add_comments:
child.add_comment("Repetitive array of:") child.add_comment("Repetitive array of:")
for t in value: for t in value:
child.marshall(name, t, False, add_comments=add_comments, ns=ns) child.marshall(name, t, False, add_comments=add_comments, ns=ns)
elif isinstance(value, basestring): # do not convert strings or unicodes elif isinstance(value, basestring): # do not convert strings or unicodes
self.add_child(name, value,ns=ns) self.add_child(name, value, ns=ns)
elif value is None: # sent a empty tag? elif value is None: # sent a empty tag?
self.add_child(name, ns=ns) self.add_child(name, ns=ns)
elif value in TYPE_MAP.keys(): elif value in TYPE_MAP.keys():
# add commented placeholders for simple tipes (for examples/help only) # 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]) 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) # get special serialization function (if any)
fn = TYPE_MARSHAL_FN.get(type(value), str) fn = TYPE_MARSHAL_FN.get(type(value), str)
self.add_child(name, fn(value), ns=ns) self.add_child(name, fn(value), ns=ns)
+73 -41
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/python
# -*- coding: latin-1 -*- # -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify # 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 # 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 # 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 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details. # 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 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__) 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. # We store metadata about what available transport mechanisms we have available.
# #
_http_connectors = {} # libname: classimpl mapping _http_connectors = {} # libname: classimpl mapping
_http_facilities = {} # functionalitylabel: [sequence of libname] mapping _http_facilities = {} # functionalitylabel: [sequence of libname] mapping
class TransportBase: class TransportBase:
@classmethod @classmethod
@@ -45,20 +61,34 @@ class TransportBase:
# #
try: try:
import httplib2 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: except ImportError:
TIMEOUT = None # timeout not supported by urllib2 TIMEOUT = None # timeout not supported by urllib2
pass pass
else: else:
class Httplib2Transport(httplib2.Http, TransportBase): class Httplib2Transport(httplib2.Http, TransportBase):
_wrapper_version = "httplib2 %s" % httplib2.__version__ _wrapper_version = "httplib2 %s" % httplib2.__version__
_wrapper_name = 'httplib2' _wrapper_name = 'httplib2'
def __init__(self, timeout, proxy=None, cacert=None, sessions=False): def __init__(self, timeout, proxy=None, cacert=None, sessions=False):
##httplib2.debuglevel=4 ##httplib2.debuglevel=4
kwargs = {} kwargs = {}
if proxy: if proxy:
import socks import socks
kwargs['proxy_info'] = httplib2.ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, **proxy) 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 # set optional parameters according supported httplib2 version
if httplib2.__version__ >= '0.3.0': if httplib2.__version__ >= '0.3.0':
@@ -76,15 +106,15 @@ else:
if 'timeout' in inspect.getargspec(httplib2.Http.__init__)[0]: if 'timeout' in inspect.getargspec(httplib2.Http.__init__)[0]:
_http_facilities.setdefault('timeout', []).append('httplib2') _http_facilities.setdefault('timeout', []).append('httplib2')
# #
# urllib2 support. # urllib2 support.
# #
import urllib2
class urllib2Transport(TransportBase): class urllib2Transport(TransportBase):
_wrapper_version = "urllib2 %s" % urllib2.__version__ _wrapper_version = "urllib2 %s" % urllib2.__version__
_wrapper_name = 'urllib2' _wrapper_name = 'urllib2'
def __init__(self, timeout=None, proxy=None, cacert=None, sessions=False): def __init__(self, timeout=None, proxy=None, cacert=None, sessions=False):
import sys
if (timeout is not None) and not self.supports_feature('timeout'): if (timeout is not None) and not self.supports_feature('timeout'):
raise RuntimeError('timeout is not supported with urllib2 transport') raise RuntimeError('timeout is not supported with urllib2 transport')
if proxy: if proxy:
@@ -94,7 +124,6 @@ class urllib2Transport(TransportBase):
self.request_opener = urllib2.urlopen self.request_opener = urllib2.urlopen
if sessions: if sessions:
from cookielib import CookieJar
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar())) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
self.request_opener = opener.open self.request_opener = opener.open
@@ -104,16 +133,17 @@ class urllib2Transport(TransportBase):
req = urllib2.Request(url, body, headers) req = urllib2.Request(url, body, headers)
try: try:
f = self.request_opener(req, timeout=self._timeout) 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: if f.code != 500:
raise raise
return f.info(), f.read() return f.info(), f.read()
_http_connectors['urllib2'] = urllib2Transport _http_connectors['urllib2'] = urllib2Transport
_http_facilities.setdefault('sessions', []).append('urllib2') _http_facilities.setdefault('sessions', []).append('urllib2')
import sys import sys
if sys.version_info >= (2,6): if sys.version_info >= (2, 6):
_http_facilities.setdefault('timeout', []).append('urllib2') _http_facilities.setdefault('timeout', []).append('urllib2')
del sys del sys
@@ -129,11 +159,15 @@ else:
try: try:
from cStringIO import StringIO from cStringIO import StringIO
except ImportError: except ImportError:
from StringIO import StringIO try:
from StringIO import StringIO
except ImportError:
from io import StringIO
class pycurlTransport(TransportBase): class pycurlTransport(TransportBase):
_wrapper_version = pycurl.version _wrapper_version = pycurl.version
_wrapper_name = 'pycurl' _wrapper_name = 'pycurl'
def __init__(self, timeout, proxy=None, cacert=None, sessions=False): def __init__(self, timeout, proxy=None, cacert=None, sessions=False):
self.timeout = timeout self.timeout = timeout
self.proxy = proxy or {} self.proxy = proxy or {}
@@ -141,7 +175,7 @@ else:
def request(self, url, method, body, headers): def request(self, url, method, body, headers):
c = pycurl.Curl() c = pycurl.Curl()
c.setopt(pycurl.URL, str(url)) c.setopt(pycurl.URL, url)
if 'proxy_host' in self.proxy: if 'proxy_host' in self.proxy:
c.setopt(pycurl.PROXY, self.proxy['proxy_host']) c.setopt(pycurl.PROXY, self.proxy['proxy_host'])
if 'proxy_port' in self.proxy: if 'proxy_port' in self.proxy:
@@ -154,20 +188,19 @@ else:
#self.body = StringIO(body) #self.body = StringIO(body)
#c.setopt(pycurl.HEADERFUNCTION, self.header) #c.setopt(pycurl.HEADERFUNCTION, self.header)
if self.cacert: 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_VERIFYPEER, self.cacert and 1 or 0)
c.setopt(pycurl.SSL_VERIFYHOST, self.cacert and 2 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) c.setopt(pycurl.TIMEOUT, self.timeout)
if method=='POST': if method == 'POST':
c.setopt(pycurl.POST, 1) c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, body) c.setopt(pycurl.POSTFIELDS, body)
if headers: if headers:
hdrs = ['%s: %s' % (str(k), str(v)) for k, v in headers.items()] hdrs = ['%s: %s' % (k, v) for k, v in headers.items()]
##print hdrs log.debug(hdrs)
c.setopt(pycurl.HTTPHEADER, hdrs) c.setopt(pycurl.HTTPHEADER, hdrs)
c.perform() c.perform()
##print "pycurl perform..."
c.close() c.close()
return {}, self.buf.getvalue() return {}, self.buf.getvalue()
@@ -178,15 +211,15 @@ else:
class DummyTransport: class DummyTransport:
"Testing class to load a xml response" """Testing class to load a xml response"""
def __init__(self, xml_response): def __init__(self, xml_response):
self.xml_response = xml_response self.xml_response = xml_response
def request(self, location, method, body, headers): def request(self, location, method, body, headers):
print method, location log.debug("%s %s", method, location)
print headers log.debug(headers)
print body log.debug(body)
return {}, self.xml_response return {}, self.xml_response
@@ -222,20 +255,19 @@ def get_http_wrapper(library=None, features=[]):
else: else:
return _http_connectors[candidate_name] return _http_connectors[candidate_name]
def set_http_wrapper(library=None, features=[]): def set_http_wrapper(library=None, features=[]):
"Set a suitable HTTP connection wrapper." """Set a suitable HTTP connection wrapper."""
global Http global Http
Http = get_http_wrapper(library, features) Http = get_http_wrapper(library, features)
return Http return Http
def get_Http(): def get_Http():
"Return current transport class" """Return current transport class"""
global Http global Http
return Http return Http
# define the default HTTP connection class (it can be changed at runtime!): # define the default HTTP connection class (it can be changed at runtime!):
set_http_wrapper() set_http_wrapper()