Setting custom headers on a python-wordpress-xmlrpc request

I’m trying to connect to a WordPress blog using XMLRPC. I’m using the latest library, v2.3 (http://python-wordpress-xmlrpc.readthedocs.org/en/latest/).

I get the following exception when I try to initialize the client:

Read More
ServerConnectionError: <ProtocolError for www.myblogaddress.com/xmlrpc.php: 403 Forbidden>

I noticed that this happens before the username & password are checked, so it doesn’t have anything to do with invalid credentials. I believe it might require some custom headers, like user agent, but I don’t know how to set a custom transport param.

I have copied the code from the python-wordpress-xmlrpc library and modified it so I could make tests. Here is what I have so far:

from xmlrpclib import Transport

class SpecialTransport(Transport):

    def send_content(self, connection, request_body):

        connection.putheader("Content-Type", "text/xml")
        connection.putheader("Content-Length", str(len(request_body)))
        connection.putheader('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11')

        connection.putheader('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
        connection.putheader('Accept-Charset','ISO-8859-1,utf-8;q=0.7,*;q=0.3')
        connection.putheader('Accept-Encoding','none')
        connection.putheader('Accept-Language', 'en-US,en;q=0.8')
        connection.putheader('Connection', 'keep-alive')

        connection.endheaders()
        if request_body:
            connection.send(request_body)

url = "{test_url_here}"

try:
    server = xmlrpc_client.ServerProxy(url, allow_none=True, transport=SpecialTransport())
    supported_methods = server.mt.supportedMethods()

except xmlrpc_client.ProtocolError as err:

    print "A protocol error occurred"
    print "URL: %s" % err.url
    print "HTTP/HTTPS headers: %s" % err.headers
    print "Error code: %d" % err.errcode
    print "Error message: %s" % err.errmsg

I should mention I have successfully connected to the same blog from a PHP script, so I believe it has something to do with the Python request. Any idea why this doesn’t work?

Thanks for the help!

Related posts

Leave a Reply

2 comments

  1. I figured that the “403 Forbidden” status was because the XMLRPC api didn’t “like” the user agent. Like I said, the same request was working fine using a PHP script.

    In the xmlrpclib Transport class, the user agent is set as:

    user_agent = "xmlrpclib.py/%s (by www.pythonware.com)" % __version__
    

    My send_content() method was not overwriting it, instead my request ended up with 2 User-Agent headers. Maybe someone can shed some light on what this happens.

    So, I did the following:

    from xmlrpclib import Transport
    
    class SpecialTransport(Transport):
    
        user_agent = 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31'
    
    try:
        # Use the verbose flag for debugging
        server = xmlrpc_client.ServerProxy(url, transport=SpecialTransport(), verbose=True)
    
    except xmlrpc_client.ProtocolError as err:
        print "A protocol error occurred"
        print "URL: %s" % err.url
        print "HTTP/HTTPS headers: %s" % err.headers
        print "Error code: %d" % err.errcode
        print "Error message: %s" % err.errmsg
    

    It worked just fine after that.

    After debugging the xmlrpclib problem, my code ended up as:

    from xmlrpclib import Transport
    import wordpress_xmlrpc
    
    class SpecialTransport(Transport):
    
        user_agent = 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31'
    
    wordpress_client = wordpress_xmlrpc.Client(
                'http://www.myblogaddress.com/xmlrpc.php',
                'username',
                'password',
                transport=SpecialTransport()
            )