如何使用python xmlrpclib为RPC调用发送自定义http_headers?

sha*_*pan 2 python xml-rpc xmlrpclib http-headers

如何HTTP Headers使用python xmlrpclib库发送自定义!?我需要http_headers在调用RPC方法时发送一些特殊的自定义.

Try*_*yPy 11

您可以子类化xmlrpclib.Transport并将其作为参数传递给ServerProxy.选择一种方法来覆盖(我选择send_content)并且你已经设置好了.

# simple test program (from the XML-RPC specification)
from xmlrpclib import ServerProxy, Transport, Error

class SpecialTransport(Transport):

    def send_content(self, connection, request_body):

        print "Add your headers here!"

        connection.putheader("Content-Type", "text/xml")
        connection.putheader("Content-Length", str(len(request_body)))
        connection.endheaders()
        if request_body:
            connection.send(request_body)


# server = ServerProxy("http://localhost:8000") # local server
server = ServerProxy("http://betty.userland.com", transport=SpecialTransport())

print server

try:
    print server.examples.getStateName(41)
except Error, v:
    print "ERROR", v
Run Code Online (Sandbox Code Playgroud)