4

How can I send custom HTTP Headers using python xmlrpclib library ! ? I need to send some special custom http_headers at the time of calling RPC methods.

sorin
  • 161,544
  • 178
  • 535
  • 806
shahjapan
  • 13,637
  • 22
  • 74
  • 104

1 Answers1

14

You can subclass xmlrpclib.Transport and pass that as an argument to ServerProxy. Pick a method to override (I chose send_content) and you're set.

# 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
TryPyPy
  • 6,214
  • 5
  • 35
  • 63
  • I am not able to use this as I am getting an exception af the first method call, see http://stackoverflow.com/questions/17569519/unable-to-use-custom-transport-class-with-python-xmlrpclib-due-to-typeerror-unb – sorin Jul 10 '13 at 11:31