【问题标题】:Receiving XML document with python via HTTP通过 HTTP 使用 python 接收 XML 文档
【发布时间】:2010-05-25 11:00:06
【问题描述】:

我想在 python 中做一个非常简单的网络服务器,能够通过 HTTP 接收 XML 文档,然后作为响应 XML 文档发送。

你有什么例子吗?

只是为了了解如何安排工作......

非常感谢!

更新:

我需要这样的东西:

客户使用 xml 文档发布帖子: 加号2参数> 3参数> 请求>'

python 服务器回答: 好的结果> 5结果> 响应>

你有这样的例子吗??

【问题讨论】:

  • “普通”HTTP(例如 GET 请求)或类似 SOAP/XML 的构造?
  • Basic python(BaseHTTPServer 和朋友),或者像 twisted 这样的框架也受欢迎?

标签: python xml http


【解决方案1】:

你可以使用 XMLRPC :

SimpleXMLRPCServer 示例(来自 Python 文档)

服务器代码:

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)
server.register_introspection_functions()

# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)

# Register a function under a different name
def adder_function(x,y):
    return x + y
server.register_function(adder_function, 'add')

# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
    def div(self, x, y):
        return x // y

server.register_instance(MyFuncs())

# Run the server's main loop
server.serve_forever()

以下客户端代码将调用前面服务器提供的方法:

import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.pow(2,3)  # Returns 2**3 = 8
print s.add(2,3)  # Returns 5
print s.div(5,2)  # Returns 5//2 = 2

# Print list of available methods
print s.system.listMethods()

【讨论】:

    【解决方案2】:

    标准库示例:

    或者使用一些轻量级的网络框架:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2013-03-12
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多