Introduction to Web Services
1) A web service is a software system designed to support
interoperable interaction between different applications
over a network such as the internet or a private intranet.
2) It uses standardized communication protocols to allow
two or more applications — potentially written in different
programming languages and running on different
operating systems — to communicate and exchange data
seamlessly.
3) Communication between a client and a web service is
often performed using:
•Extensible Markup Language (XML) when following the
Simple Object Access Protocol (SOAP) style.
•JavaScript Object Notation (JSON) or XML when following
the Representational State Transfer (REST) style.
Purpose of Use Web Services
1) Web services enable interoperability. For example:
2) A program written in Python can communicate with a
service written in Java.
3) A Windows-based client can request data from a
service running on a Linux server.
Features of Web Services
1) Language and Operating System Independent
Applications can be built in any language and
deployed on any operating system.
2) Uses Open Standards
For example, Hypertext Transfer Protocol (HTTP),
XML, JSON, SOAP, and Web Services Description
Language (WSDL).
3) Self-Describing
Services describe themselves using XML-based
formats such as WSDL or JSON-based formats
such as OpenAPI Specification.
4) Discoverable
Services can be registered and found using
registries such as Universal Description,
Discovery, and Integration (UDDI).
Why Use Web Services?
Web services provide several important benefits:
[Link] Existing Functionality
Allow legacy applications or core systems to be accessible
to other systems over the network without rewriting code.
[Link] Between Heterogeneous Systems
Connect applications built in different programming
languages and deployed on different platforms.
[Link] Communication Protocols
Use industry-recognized standards such as SOAP over HTTP,
REST over HTTP, and XML for data structuring.
[Link]-Cost Communication
Leverage existing internet infrastructure instead of
expensive proprietary communication solutions.
[Link] Coupled Architecture
The client (service consumer) and the server (service
provider) can change independently as long as they respect
the agreed interface.
Types of Web Services
1) Simple Object Access Protocol (SOAP)
❖XML-based messaging protocol.
❖Works over multiple transport protocols such as HTTP,
Simple Mail Transfer Protocol (SMTP), and File Transfer
Protocol (FTP).
❖ Requires WSDL to describe available operations and
message formats.
2) Representational State Transfer (REST)
❖An architectural style using HTTP methods (GET, POST,
PUT, DELETE) to operate on resources.
❖Frequently returns JSON but can also return XML or
other formats.
❖Easier to use and generally faster than SOAP.
3) Extensible Markup Language - Remote Procedure Call
(XML-RPC)
❖Older XML-based mechanism for remote procedure calls.
❖Encodes method calls and responses in XML and
Core Components of Web Services
1) SOAP:
Messaging protocol that defines the XML structure
for requests and responses.
2) Web Services Description Language (WSDL):
XML-based language that describes the service
interface, available methods, parameters, and
network location.
3) Universal Description, Discovery, and Integration
(UDDI):
A registry system where web services can be
published and discovered.
4) Transport Layer:
Protocols such as HTTP, Simple Mail Transfer
Protocol (SMTP), and FTP used to transmit the
messages.
How Web Services Work – General
Flow
1) Client Application Sends a Request
❖In SOAP: Sends an XML message wrapped in the
SOAP format.
❖In Representational State Transfer(REST): Sends an
HTTP request with data as JavaScript Object
Notation(JSON) or XML.
2) Server Processes the Request
The service executes the corresponding method or
operation.
3) Server Sends a Response
❖SOAP: XML-formatted response.
❖REST: JSON or XML-formatted response.
Web Services Flow
1) Client: Python script requests data.
2) Server: Java service running on Linux returns the
data in JSON format.
Calling a Representational State
Transfer(REST): API
import requests
# Example: Public GitHub API
url = "[Link]
response = [Link](url)
if response.status_code == 200:
data = [Link]()
print("Username:", data['login'])
print("Public Repos:", data['public_repos'])
else:
print("Error:", response.status_code)
The output:
Username: octocat
Public Repos: 8
Building a REST API with Flask
from flask import Flask, jsonify, request
app = Flask(__name__)
tasks = []
@[Link]('/tasks', methods=['GET', 'POST'])
def handle_tasks():
if [Link] == 'POST':
task = [Link]('task')
[Link](task)
return jsonify({"status": "Task added!"}), 201
return jsonify({"tasks": tasks}), 200
if __name__ == '__main__':
[Link](debug=True)
Web service Testing
curl -X POST -H "Content-Type: application/json" \
-d '{"task":"Learn Python"}'
[Link]
curl [Link]
Calling a SOAP Web Service
from zeep import Client
# WSDL endpoint for the SOAP service
wsdl_url =
'[Link]
client = Client(wsdl_url)
# Call SOAP method
result = [Link](5, 7)
print("5 + 7 =", result)
Building a SOAP-like API in Python
from spyne import Application, rpc, ServiceBase, Integer
from [Link] import Soap11
from [Link] import WsgiApplication
class CalculatorService(ServiceBase):
@rpc(Integer, Integer, _returns=Integer)
def add(ctx, a, b):
return a + b
app = Application([CalculatorService], '[Link]',
in_protocol=Soap11(), out_protocol=Soap11())
if __name__ == '__main__':
from wsgiref.simple_server import make_server
server = make_server('[Link]', 8000, WsgiApplication(app))
print("SOAP service running on port 8000...")
server.serve_forever()
Comparison: SOAP vs. REST in Python
Feature SOAP (zeep) REST (requests)
Data Format XML only JSON or XML
Protocol Support HTTP, SMTP, FTP HTTP
High (strict XML Low (flexible
Complexity
schemas) formats)
Slower (more Faster (less
Speed
overhead) verbose)
Enterprise and Web and mobile
Best For
financial systems applications