A comprehensive Python SOAP client library for consuming SOAP web services with support for SOAP 1.1/1.2, WSSE authentication, and async operations
Core SOAP client functionality for connecting to web services, calling operations, and handling responses. Zeep provides synchronous, asynchronous, and caching client variants with automatic WSDL parsing and type conversion.
The primary interface for SOAP web service interactions, handling WSDL parsing, service binding, and operation execution.
class Client:
def __init__(
self,
wsdl: str,
wsse=None,
transport=None,
service_name: str = None,
port_name: str = None,
plugins: list = None,
settings=None
):
"""
Create a SOAP client for the given WSDL.
Parameters:
- wsdl: URL or file path to WSDL document
- wsse: WSSE security configuration
- transport: Custom transport instance
- service_name: Specific service name to use
- port_name: Specific port name to use
- plugins: List of plugin instances
- settings: Settings instance for configuration
"""
@property
def service(self):
"""Default ServiceProxy instance for calling operations."""
@property
def namespaces(self) -> dict:
"""WSDL namespace prefix mappings."""
def bind(self, service_name: str = None, port_name: str = None):
"""
Create ServiceProxy for specific service/port combination.
Parameters:
- service_name: Name of the service to bind to
- port_name: Name of the port to bind to
Returns:
ServiceProxy instance
"""
def create_service(self, binding_name: str, address: str):
"""
Create ServiceProxy for given binding and endpoint address.
Parameters:
- binding_name: QName of the binding
- address: Endpoint URL
Returns:
ServiceProxy instance
"""
def create_message(self, service, operation_name: str, *args, **kwargs):
"""
Create SOAP message payload for operation.
Parameters:
- service: Service instance
- operation_name: Name of operation to create message for
- args, kwargs: Operation parameters
Returns:
lxml.etree._Element: SOAP message envelope
"""
def get_type(self, name: str):
"""Get XSD type by qualified name."""
def get_element(self, name: str):
"""Get XSD element by qualified name."""
def type_factory(self, namespace: str):
"""
Return type factory for given namespace.
Parameters:
- namespace: Target namespace for type factory
Returns:
Factory instance for creating types
"""
def set_ns_prefix(self, prefix: str, namespace: str):
"""
Set namespace prefix shortcut.
Parameters:
- prefix: Short prefix to use
- namespace: Full namespace URI
"""
def set_default_soapheaders(self, headers):
"""
Set default SOAP headers for all operations.
Parameters:
- headers: Default headers to include in requests
"""Asynchronous SOAP client for non-blocking operations using httpx for HTTP transport.
class AsyncClient(Client):
"""
Asynchronous SOAP client using AsyncTransport.
All methods from Client are available as async variants.
Requires 'httpx' package for async HTTP support.
"""
async def __aenter__(self):
"""Async context manager entry."""
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit."""SOAP client with built-in response caching for improved performance.
class CachingClient(Client):
"""
Client with automatic caching of SOAP responses.
Inherits all functionality from Client with added caching capabilities.
Cache configuration is handled through Transport settings.
"""Helper for creating XSD types and elements from WSDL namespaces.
class Factory:
def __init__(self, types, kind: str, namespace: str):
"""
Create factory for XSD types/elements.
Parameters:
- types: Types registry
- kind: 'type' or 'element'
- namespace: Target namespace
"""
def __getattr__(self, key: str):
"""Get type/element by local name."""
def __getitem__(self, key: str):
"""Get type/element by local name (alternative syntax)."""from zeep import Client
# Simple SOAP service call
client = Client('http://www.dneonline.com/calculator.asmx?WSDL')
result = client.service.Add(intA=10, intB=20)
print(f"Result: {result}")
# Access service information
print("Available operations:")
for operation in client.service._binding._operations.keys():
print(f" - {operation}")from zeep import Client
client = Client('http://example.com/multi-service.wsdl')
# Use default service
default_result = client.service.DefaultOperation(param='value')
# Bind to specific service/port
specific_service = client.bind('SpecificService', 'SpecificPort')
specific_result = specific_service.SpecificOperation(param='value')
# Create service with custom endpoint
custom_service = client.create_service(
'{http://example.com}ServiceBinding',
'http://custom-endpoint.com/service'
)
custom_result = custom_service.Operation(param='value')import asyncio
from zeep import AsyncClient
async def call_soap_service():
async with AsyncClient('http://example.com/service.wsdl') as client:
result = await client.service.AsyncOperation(param='value')
return result
# Run async operation
result = asyncio.run(call_soap_service())from zeep import Client
client = Client('http://example.com/service.wsdl')
# Create complex type instance
address_type = client.get_type('{http://example.com}Address')
address = address_type(
street='123 Main St',
city='Anytown',
state='CA',
zip='12345'
)
# Use in operation
result = client.service.UpdateAddress(address=address)
# Use type factory for creating types
factory = client.type_factory('http://example.com')
person = factory.Person(name='John', age=30)
# Set namespace prefix shortcut
client.set_ns_prefix('ex', 'http://example.com')
person_type = client.get_type('ex:Person')
# Set default SOAP headers
client.set_default_soapheaders([
{'name': 'Authentication', 'value': 'Bearer token123'}
])Install with Tessl CLI
npx tessl i tessl/pypi-zeep