WEB开发网
开发学院软件开发Python Python Web 服务开发人员: 三股力量:Python、Web... 阅读

Python Web 服务开发人员: 三股力量:Python、Web 服务和 XSLT

 2008-09-30 13:02:15 来源:WEB开发网   
核心提示: XSLT 端点导入 SOAP 框架(文件 kafka/soap.xsl),然后设置该框架将要使用的参数,Python Web 服务开发人员: 三股力量:Python、Web 服务和 XSLT(2),并设置它在处理构成 SOAP 消息的整个 XML 文档的过程中将要分派的模板,全局变量 Me

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()

上一页  1 2 3 4 5 6 7  下一页

Tags:Python Web 服务

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接