How to send custom http_headers for RPC calls using python xmlrpclib?
Asked Answered
G

1

5

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.

Guilty answered 7/1, 2011 at 9:27 Comment(0)
W
15

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
Workaday answered 16/1, 2011 at 21:13 Comment(1)
I am not able to use this as I am getting an exception af the first method call, see #17570019Predatory

© 2022 - 2024 — McMap. All rights reserved.