4

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

4

1 回答 1

13

您可以子类化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
于 2011-01-16T21:13:48.550 回答