Christopher Dix 所开发的“Kafka — XSL SOAP 工具箱”(请参阅 参考资料)是一种用于构造 SOAP 端点的 XSLT 框架。它只涵盖了 SOAP 1.1,但 Kafka 端点演示了传递 UserLand SOAP 验证器(UserLand SOAP Validator)的能力,并且根据 SOAP 1.2 对它进行更新似乎并不太困难。 清单 1展示了一个样本 Kafka 端点:求两数之和的 SOAP 服务器(一个典型而简单的 SOAP 样本)。
清单 1. 求两数之和的 Kafka SOAP 端点
Add http://www.topxml.com/
XSLT 端点导入 SOAP 框架(文件 kafka/soap.xsl),然后设置该框架将要使用的参数,并设置它在处理构成 SOAP 消息的整个 XML 文档的过程中将要分派的模板。全局变量 Method 和 MethodNS 声明了组成消息的 XML 元素。在处理完 SOAP 信封之后,该框架调用 ProcessPayload 模板,该模板传入了 XML 主体的有效负载。 xsl:for-each 是将上下文切换成想要的节点的标准技巧。参数 A 和 B 是使用简单 XPaths 从这个元素读取的,而框架被再次调用以帮助写出响应参数。 WriteParameter 模板让您指定元素名称、数据类型和每个 输出参数的值。本示例中的响应值是将两个输入参数相加所得的结果。
将这个端点部署为服务器相当于设置一个 HTTP 侦听器。Python 的 BaseHTTPServer 模块向您提供了所需的机制,能够轻而易举地处理该协议的 HTTP 部分。请参阅 清单 2。
清单 2. 用于清单 1 中所实现的 Kafka SOAP 端点的 Python HTTP 框架
#HTTP Listener code for SOAP server import BaseHTTPServer #The processor class is the core of the XSLT API from Ft.Xml.Xslt import Processor #4XSLT uses an InputSource system for reading XML from Ft.Xml import InputSource SOAP_IMPL_FILE = "add.xsl" class KafkaSoapHandler(BaseHTTPServer.BaseHTTPRequestHandler): def init(cls): from Ft.Lib import Uri #Set up a processor instance to use KafkaSoapHandler.processor = Processor.Processor() #Load it with add.xsl add_uri = Uri.OsPathToUri(SOAP_IMPL_FILE, attemptAbsolute=1) transform = InputSource.DefaultFactory.fromUri(add_uri) KafkaSoapHandler.processor.appendStylesheet(transform) #Now the processor is prepped with a transform and can be used #over and over for the same transform return #Make init() a static method of the class init = classmethod(init) def do_POST(self): clen = self.headers.getheader('content-length') if clen: clen = int(clen) else: print 'POST ERROR: missing content-length' return if self.path != '/add': self.send_error(404) input_body = self.rfile.read(clen) #input_body is the request SOAP envelope and contents response_body = self._run_through_kafka(input_body) #response_body is the response SOAP envelope and contents self._send_response(200, 'OK', response_body) return def _run_through_kafka(self, body): #In 4Suite all InputSources have base URIs in case they refer to #other URIs in some way and resolution is required. #The SOAP messages will not have any such URI references, #So use a dummy base URI source = InputSource.DefaultFactory.fromString(body, "urn:dummy") response = self.processor.run(source) return response def _send_response(self, code, msg, body): #Prepare a normal response self.send_response(200, 'OK') #Send standard HTP headers self.send_header('Content-type','text/html; charset=utf-8') self.send_header("Connection", "close") self.send_header("Accept-Ranges", "bytes") self.send_header('Content-length', len(body)-1) self.end_headers() #Send the response prepared by the SOAP end point self.wfile.write(body) return listen_on_port = 8888 #Set up to run on local machine server_address = ('127.0.0.1', listen_on_port) KafkaSoapHandler.init() httpd = BaseHTTPServer.HTTPServer(server_address, KafkaSoapHandler) print "Listening on port", listen_on_port #Go into a the main event loop httpd.serve_forever()
我们详细地注释了该清单,因此它应该是易于理解的。请注意,这段代码非常简单,这是因为它仅需处理该协议的 HTTP 部分,而将 XML 和 SOAP 部分的工作交由 Kafka 框架完成。该服务器专用于一个端点,因此它只须对 XSLT 转换进行一次解析和设置,然后它就可以简单地反复为每次新的请求运行该转换。这就是将处理器设置迁移到特殊的类方法中的原因,处理程序一注册到服务器就立即调用该方法。 classmethod 内置方法是 Python 2.2 中的新功能,实际上该版本是本例和后面的示例所必需的版本。它提供了隐式类对象 (cls) ,您可以将静态数据(如已准备好的处理器实例)附加到该对象上,然后通常可以通过普通方法上的 self 实例引用来使用该数据。
我们使用 SOAPpy 0.10.1 的最新发行版(请参阅 参考资料)测试了该端点,该发行版具有许多很棒的新功能,稍后我们将在本专栏中进行讨论。 清单 3是使用该端点的 SOAPpy 客户机。打开一个命令 shell 并为服务器运行 python listing2.py。然后打开另一个 shell 并运行 python listing3.py,该命令将报告正确的响应,形如 Add result: 7.0。
清单 3: 用于求两数之和的 SOAPpy 客户机
import SOAPpy ENDPOINT = "http://localhost:8888/add" ADD_NS = "http://www.topxml.com/" remote = SOAPpy.SOAPProxy(ENDPOINT, namespace=ADD_NS) print "Add result:", remote.Add(A=3, B=4)
使用描述
正如我们先前所说的,不仅 XML 中的有效负载是有用的 Web 服务特性,描述也是有用的特性。 清单 4是一个用于添加服务的 WSDL 文件,它是根据 Christopher Dix 的原始文件修改而得到的。它是 WSDL 1.1 版本的。
清单 4. 用于添加服务的 WSDL
清单 5提供了一个为端点用户呈现有用信息的 XSLT 脚本。它是从先前的 developerWorks 文章“WSDL processing with XSLT”(请参阅 参考资料)中所开发的一个转换改编而来的。它使用了许多自由方式(liberty)和快捷方式(尤其是在它处理 WSDL 上下文中的限定名时),但它也许可用于目前使用的大多数 WSDL 1.1 文件。
清单 5. XSLT 脚本
Service summary:Service summary:
Service " " hosted at
查看更多关于使用Python下的XSLTAPI进行web开发的简单教程的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did88434