python 如何在python中发送xml-rpc请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2242174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:09:09  来源:igfitidea点击:

How to send a xml-rpc request in python?

pythonxml-rpc

提问by John Jiang

I was just wondering, how would I be able to send a xml-rpcrequest in python? I know you can use xmlrpclib, but how do I send out a request in xmlto access a function?

我只是想知道,我如何能够xml-rpc在 python 中发送请求?我知道您可以使用xmlrpclib,但是如何发出请求xml以访问某个功能?

I would like to see the xmlresponse.

我想看看xml回应。

So basically I would like to send the following as my request to the server:

所以基本上我想将以下内容作为我的请求发送到服务器:

<?xml version="1.0"?>
<methodCall>
  <methodName>print</methodName>
  <params>
    <param>
        <value><string>Hello World!</string></value>
    </param>
  </params>
</methodCall>

and get back the response

并得到回复

回答by Eli Bendersky

Here's a simple XML-RPC client in Python:

这是一个用 Python 编写的简单 XML-RPC 客户端:

import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.myfunction(2, 4)

Works with this server:

与此服务器一起使用:

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)

def myfunction(x, y):
    status = 1
    result = [5, 6, [4, 5]]
    return (status, result)
server.register_function(myfunction)

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

To access the guts of xmlrpclib, i.e. looking at the raw XML requests and so on, look up the xmlrpclib.Transportclass in the documentation.

要访问 的内容xmlrpclib,即查看原始 XML 请求等,请xmlrpclib.Transport在文档中查找该类。

回答by Nicholas Clarke

I have pared down the source code in xmlrpc.clientto a minimum required to send a xml rpc request (as I was interested in trying to port the functionality). It returns the response XML.

我已将xmlrpc.client 中的源代码减少到发送 xml rpc 请求所需的最低限度(因为我对尝试移植功能感兴趣)。它返回响应 XML。

Server:

服务器:

from xmlrpc.server import SimpleXMLRPCServer

def is_even(n):
    return n%2 == 0

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(is_even, "is_even")
server.serve_forever() 

Client:

客户:

import http.client

request_body = b"<?xml version='1.0'?>\n<methodCall>\n<methodName>is_even</methodName>\n<params>\n<param>\n<value><int>2</int></value>\n</param>\n</params>\n</methodCall>\n"

connection = http.client.HTTPConnection('localhost:8000')
connection.putrequest('POST', '/')
connection.putheader('Content-Type', 'text/xml')
connection.putheader('User-Agent', 'Python-xmlrpc/3.5')
connection.putheader("Content-Length", str(len(request_body)))
connection.endheaders(request_body)

print(connection.getresponse().read())

回答by Alex Martelli

What do you mean by "get around"? xmlrpclibis thenormal way to write an XML-RPC client in python. Just look at the sources(or copy them to your own module and add printstatements!-) if you want to know the details of how things are done.

你说的“绕道”是什么意思? xmlrpclib是在 python 中编写 XML-RPC 客户端正常方法。如果您想知道事情是如何完成的细节,只需查看源代码(或将它们复制到您自己的模块并添加print语句!-)。