Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 628
shahjapan Avatar asked Dec 12 '22 16:12

shahjapan


1 Answers

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
like image 73
TryPyPy Avatar answered Dec 15 '22 06:12

TryPyPy