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

发布于 2024-08-21 05:54:31 字数 488 浏览 8 评论 0原文

我只是想知道,如何在 python 中发送 xml-rpc 请求?我知道您可以使用 xmlrpclib,但是如何在 xml 中发送请求来访问函数?

我想查看 xml 响应。

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

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

并获取响应

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

I would like to see the xml response.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-08-28 05:54:31

下面是一个简单的 Python 中的 XML-RPC 客户端:

import xmlrpclib

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

与此服务器配合使用:

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

要访问 xmlrpclib 的内部结构,即查看原始 XML 请求等,请查找 xmlrpclib.Transport< /code> 文档中的类。

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

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.Transport class in the documentation.

遮了一弯 2024-08-28 05:54:31

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

服务器:

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

客户端:

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

I have pared down the source code in xmlrpc.client to a minimum required to send a xml rpc request (as I was interested in trying to port the functionality). It returns the response 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())
Oo萌小芽oO 2024-08-28 05:54:31

你所说的“绕过”是什么意思? xmlrpclib用 python 编写 XML-RPC 客户端的正常方法。只需查看来源 (或者将它们复制到您自己的模块中并添加 print 语句!-)如果您想了解事情如何完成的详细信息。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文